Skip to content

Commit

Permalink
let's goooo
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 21, 2024
1 parent 762710f commit 139d541
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 5 deletions.
1 change: 1 addition & 0 deletions next-enterprise/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRET='your-secret'
1 change: 1 addition & 0 deletions next-enterprise/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ yarn-error.log*

# env files (can opt-in for committing if needed)
.env*
!.env.local.example

# vercel
.vercel
Expand Down
2 changes: 2 additions & 0 deletions next-enterprise/expirator-service/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SECRET='your-secret'
TOKEN='sk...
176 changes: 176 additions & 0 deletions next-enterprise/expirator-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local
!.env.local.example

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
Binary file added next-enterprise/expirator-service/bun.lockb
Binary file not shown.
44 changes: 44 additions & 0 deletions next-enterprise/expirator-service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {createClient} from '@sanity/client'

const client = createClient({
projectId: 'hiomol4a',
dataset: 'lcapi',
apiVersion: '2024-11-21',
useCdn: false,
})

const appOrigin = 'https://lcapi-examples-next-enterprise.sanity.dev'
const appRoute = new URL('/api/expire-tags', appOrigin)
appRoute.searchParams.set('secret', process.env.SECRET!)

await new Promise((resolve, reject) => {
client.live.events().subscribe({
next: (event) => {
if (event.type === 'welcome') {
console.info(
`Connected to the Sanity Live Content API, events will be forwarded to ${appOrigin} on a protected route which expires tag entries`,
)
} else if (event.type === 'message') {
const url = new URL(appRoute)
for (const tag of event.tags) {
url.searchParams.append('tag', tag)
}
fetch(url, {method: 'POST'})
.then((res) => {
if (!res.ok) {
throw new TypeError('Failed', {cause: res.statusText})
}
return res.json()
})
.then((json) => console.info('Forwarded tags to api route', event.tags, json))
.catch((reason) =>
console.error('Failed to forward tags to api route', event, reason, url.toString()),
)
}
},
error: (error: unknown) => {
reject(error)
},
complete: () => resolve(undefined),
})
})
17 changes: 17 additions & 0 deletions next-enterprise/expirator-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "expirator-service",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"scripts": {
"start": "bun run index.ts"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@sanity/client": "^6.22.5"
}
}
27 changes: 27 additions & 0 deletions next-enterprise/expirator-service/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
5 changes: 0 additions & 5 deletions next-enterprise/src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import type {SyncTag} from '@sanity/client'
import {expireTag} from 'next/cache'

export async function expireTags(tags: SyncTag[]) {
expireTag(...tags)
console.log(`<SanityLive /> expired tags: ${tags.join(', ')}`)
}

export async function randomColorTheme(tags: SyncTag[]) {
const res = await fetch('https://lcapi-examples-api.sanity.dev/api/random-color-theme', {
method: 'PUT',
Expand Down
14 changes: 14 additions & 0 deletions next-enterprise/src/app/api/expire-tags/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {expireTag} from 'next/cache'

export async function POST(request: Request) {
const url = new URL(request.url)
const secret = url.searchParams.get('secret')
if (secret !== process.env.SECRET) {
return Response.json({error: 'Unauthorized'}, {status: 401})
}

const tags = url.searchParams.getAll('tag')
console.log('Expiring tags from expirator service', tags)
expireTag(...tags)
return Response.json({tags})
}

0 comments on commit 139d541

Please sign in to comment.