@sanity/preview-url-secret
Advanced tools
Comparing version 1.6.22-canary.5 to 1.6.22-canary.6
import { uuid } from '@sanity/uuid'; | ||
import { apiVersion, SECRET_TTL, schemaIdPrefix, schemaType, tag, deleteExpiredSecretsQuery } from './_chunks-es/constants.js'; | ||
import { apiVersion, SECRET_TTL, schemaIdPrefix, schemaIdSingleton, schemaType, tag, deleteExpiredSecretsQuery } from './constants.js'; | ||
@@ -25,6 +25,16 @@ function generateUrlSecret() { | ||
const patch = client.patch(_id).set({ secret: newSecret, source, studioUrl, userId }); | ||
await client.transaction().createOrReplace({ _id, _type: schemaType }).patch(patch).commit({ tag }); | ||
await client.transaction().createIfNotExists({ | ||
_id: schemaIdSingleton, | ||
_type: schemaType, | ||
source, | ||
studioUrl, | ||
userId | ||
}).createOrReplace({ _id, _type: schemaType }).patch(patch).commit({ tag }); | ||
return { secret: newSecret, expiresAt }; | ||
} finally { | ||
await client.delete({ query: deleteExpiredSecretsQuery }); | ||
try { | ||
await client.delete({ query: deleteExpiredSecretsQuery }); | ||
} catch (err) { | ||
console.error("Failed to delete expired secrets", err); | ||
} | ||
} | ||
@@ -31,0 +41,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { urlSearchParamPreviewSecret, urlSearchParamPreviewPathname } from './_chunks-es/constants.js'; | ||
import { urlSearchParamPreviewSecret, urlSearchParamPreviewPathname } from './constants.js'; | ||
@@ -3,0 +3,0 @@ function definePreviewUrl(options) { |
@@ -1,2 +0,2 @@ | ||
import { urlSearchParamPreviewPathname } from './_chunks-es/constants.js'; | ||
import { urlSearchParamPreviewPathname } from './constants.js'; | ||
@@ -3,0 +3,0 @@ function getRedirectTo(url) { |
@@ -1,2 +0,2 @@ | ||
import { apiVersion, urlSearchParamPreviewSecret, urlSearchParamPreviewPathname, fetchSecretQuery, tag, isDev } from './_chunks-es/constants.js'; | ||
import { apiVersion, urlSearchParamPreviewSecret, urlSearchParamPreviewPathname, fetchSecretQuery, tag, isDev } from './constants.js'; | ||
@@ -3,0 +3,0 @@ function createClientWithConfig(client) { |
import { defineType, definePlugin } from 'sanity'; | ||
import { schemaType, SECRET_TTL } from './_chunks-es/constants.js'; | ||
import { LockIcon, CloseCircleIcon, CheckmarkCircleIcon } from '@sanity/icons'; | ||
import { schemaType, schemaIdSingleton, SECRET_TTL } from './constants.js'; | ||
import { LockIcon, ApiIcon, CloseCircleIcon, CheckmarkCircleIcon } from '@sanity/icons'; | ||
@@ -15,3 +15,4 @@ const debugUrlSecretsType = defineType({ | ||
name: "secret", | ||
title: "Secret" | ||
title: "Secret", | ||
hidden: ({ document }) => document?._id === schemaIdSingleton | ||
}, | ||
@@ -36,2 +37,3 @@ { | ||
select: { | ||
_id: "_id", | ||
source: "source", | ||
@@ -42,2 +44,9 @@ studioUrl: "studioUrl", | ||
prepare(data) { | ||
if (data?._id === schemaIdSingleton) { | ||
return { | ||
title: "@sanity/preview-url-secret is setup correctly", | ||
subtitle: "Never expires", | ||
media: ApiIcon | ||
}; | ||
} | ||
const url = data.studioUrl ? new URL(data.studioUrl, location.origin) : void 0; | ||
@@ -44,0 +53,0 @@ const updatedAt = new Date(data.updatedAt).getTime(); |
@@ -1,2 +0,2 @@ | ||
import { urlSearchParamPreviewPathname, urlSearchParamPreviewSecret } from './_chunks-es/constants.js'; | ||
import { urlSearchParamPreviewPathname, urlSearchParamPreviewSecret } from './constants.js'; | ||
@@ -3,0 +3,0 @@ function withoutSecretSearchParams(url) { |
{ | ||
"name": "@sanity/preview-url-secret", | ||
"version": "1.6.22-canary.5", | ||
"version": "1.6.22-canary.6", | ||
"homepage": "https://github.com/sanity-io/visual-editing/tree/main/packages/preview-url-secret#readme", | ||
@@ -24,2 +24,8 @@ "bugs": { | ||
}, | ||
"./constants": { | ||
"source": "./src/_exports/constants.ts", | ||
"import": "./dist/constants.js", | ||
"require": "./dist/constants.cjs", | ||
"default": "./dist/constants.js" | ||
}, | ||
"./create-secret": { | ||
@@ -62,2 +68,5 @@ "source": "./src/_exports/create-secret.ts", | ||
"*": { | ||
"constants": [ | ||
"./dist/constants.d.ts" | ||
], | ||
"create-secret": [ | ||
@@ -107,5 +116,5 @@ "./dist/create-secret.d.ts" | ||
"@repo/channels": "0.4.0", | ||
"@repo/eslint-config": "0.0.0", | ||
"@repo/package.config": "0.0.0", | ||
"@repo/prettier-config": "0.0.0" | ||
"@repo/prettier-config": "0.0.0", | ||
"@repo/eslint-config": "0.0.0" | ||
}, | ||
@@ -112,0 +121,0 @@ "peerDependencies": { |
@@ -10,2 +10,5 @@ import type {PreviewUrlSecretSchemaIdPrefix, PreviewUrlSecretSchemaType} from './types' | ||
/** @internal */ | ||
export const schemaIdSingleton = `drafts.${schemaIdPrefix}` as const | ||
/** @internal */ | ||
export const apiVersion = '2023-11-09' | ||
@@ -30,3 +33,3 @@ | ||
export const fetchSecretQuery = | ||
/* groq */ `*[_type == "${schemaType}" && secret == $secret && dateTime(_updatedAt) > dateTime(now()) - ${SECRET_TTL}][0]{ | ||
/* groq */ `*[_type == "${schemaType}" && _id in path("${schemaIdPrefix}.**") && secret == $secret && dateTime(_updatedAt) > dateTime(now()) - ${SECRET_TTL}][0]{ | ||
_id, | ||
@@ -40,3 +43,3 @@ _updatedAt, | ||
export const deleteExpiredSecretsQuery = | ||
/* groq */ `*[_type == "${schemaType}" && dateTime(_updatedAt) <= dateTime(now()) - ${SECRET_TTL}]` as const | ||
/* groq */ `*[_type == "${schemaType}" && _id in path("${schemaIdPrefix}.**") && defined(secret) && dateTime(_updatedAt) <= dateTime(now()) - ${SECRET_TTL}]` as const | ||
@@ -43,0 +46,0 @@ /** |
@@ -7,2 +7,3 @@ import type {SanityClient} from '@sanity/client' | ||
schemaIdPrefix, | ||
schemaIdSingleton, | ||
schemaType, | ||
@@ -30,8 +31,24 @@ SECRET_TTL, | ||
const patch = client.patch(_id).set({secret: newSecret, source, studioUrl, userId}) | ||
await client.transaction().createOrReplace({_id, _type: schemaType}).patch(patch).commit({tag}) | ||
await client | ||
.transaction() | ||
.createIfNotExists({ | ||
_id: schemaIdSingleton, | ||
_type: schemaType, | ||
source, | ||
studioUrl, | ||
userId, | ||
}) | ||
.createOrReplace({_id, _type: schemaType}) | ||
.patch(patch) | ||
.commit({tag}) | ||
return {secret: newSecret, expiresAt} | ||
} finally { | ||
// Garbage collect expired secrets | ||
await client.delete({query: deleteExpiredSecretsQuery}) | ||
try { | ||
// Garbage collect expired secrets | ||
await client.delete({query: deleteExpiredSecretsQuery}) | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error('Failed to delete expired secrets', err) | ||
} | ||
} | ||
@@ -38,0 +55,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
142151
66
1458