@sanity/blueprints 0.26.1 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/dist/definers/functions/durable.d.ts +0 -1
- package/dist/definers/functions/durable.js +5 -5
- package/dist/definers/functions/durable.js.map +1 -1
- package/dist/definers/functions/queue.js +3 -3
- package/dist/definers/functions/queue.js.map +1 -1
- package/dist/resolve/by-name.d.ts +7 -0
- package/dist/resolve/by-name.js +13 -0
- package/dist/resolve/by-name.js.map +1 -0
- package/dist/resolve/import.d.ts +16 -0
- package/dist/resolve/import.js +48 -0
- package/dist/resolve/import.js.map +1 -0
- package/dist/resolve/index.d.ts +18 -0
- package/dist/resolve/index.js +19 -0
- package/dist/resolve/index.js.map +1 -0
- package/dist/resolve/load.d.ts +53 -0
- package/dist/resolve/load.js +155 -0
- package/dist/resolve/load.js.map +1 -0
- package/dist/resolve/stack-config.d.ts +49 -0
- package/dist/resolve/stack-config.js +108 -0
- package/dist/resolve/stack-config.js.map +1 -0
- package/dist/resolve/stack.d.ts +23 -0
- package/dist/resolve/stack.js +91 -0
- package/dist/resolve/stack.js.map +1 -0
- package/dist/resolve/types.d.ts +165 -0
- package/dist/resolve/types.js +2 -0
- package/dist/resolve/types.js.map +1 -0
- package/dist/types/functions/index.d.ts +64 -12
- package/dist/types/installation-config.d.ts +2 -0
- package/dist/types/resources.d.ts +11 -5
- package/dist/utils/debounce.d.ts +20 -0
- package/dist/utils/debounce.js +46 -0
- package/dist/utils/debounce.js.map +1 -0
- package/dist/utils/parse-duration.d.ts +9 -0
- package/dist/utils/parse-duration.js +30 -0
- package/dist/utils/parse-duration.js.map +1 -0
- package/dist/validation/functions.d.ts +9 -0
- package/dist/validation/functions.js +67 -10
- package/dist/validation/functions.js.map +1 -1
- package/package.json +19 -7
- package/src/definers/functions/durable.ts +5 -5
- package/src/definers/functions/queue.ts +3 -3
- package/src/resolve/by-name.ts +11 -0
- package/src/resolve/import.ts +52 -0
- package/src/resolve/index.ts +33 -0
- package/src/resolve/load.ts +166 -0
- package/src/resolve/stack-config.ts +116 -0
- package/src/resolve/stack.ts +100 -0
- package/src/resolve/types.ts +179 -0
- package/src/types/functions/index.ts +76 -21
- package/src/types/installation-config.ts +3 -0
- package/src/types/resources.ts +11 -5
- package/src/utils/debounce.ts +53 -0
- package/src/utils/parse-duration.ts +38 -0
- package/src/validation/functions.ts +79 -13
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {existsSync, readFileSync} from 'node:fs'
|
|
2
|
+
import {resolve} from 'node:path'
|
|
3
|
+
import {cwd, env as processEnv} from 'node:process'
|
|
4
|
+
import type {BlueprintsApiConfig} from '../index.js'
|
|
5
|
+
import type {ConfigSource, StackConfig, StackConfigFile, StackIdSources, StackIds} from './types.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The directory next to the blueprint file that holds the Stack config
|
|
9
|
+
* @internal
|
|
10
|
+
* @private
|
|
11
|
+
* @category Resolve
|
|
12
|
+
*/
|
|
13
|
+
export const STACK_CONFIG_DIR = '.sanity'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The file in {@link STACK_CONFIG_DIR} that holds the Stack ids
|
|
17
|
+
* @internal
|
|
18
|
+
* @private
|
|
19
|
+
* @category Resolve
|
|
20
|
+
*/
|
|
21
|
+
export const STACK_CONFIG_FILE = 'blueprint.config.json'
|
|
22
|
+
|
|
23
|
+
const ID_KEYS: (keyof BlueprintsApiConfig)[] = ['organizationId', 'projectId', 'stackId']
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Read `.sanity/blueprint.config.json` from a blueprint directory
|
|
27
|
+
* @param dir The blueprint directory. Defaults to the current working directory.
|
|
28
|
+
* @returns The ids in the file and its path, or `undefined` when the file does not exist
|
|
29
|
+
* @throws When the file exists and is not valid JSON
|
|
30
|
+
* @internal
|
|
31
|
+
* @private
|
|
32
|
+
* @category Resolve
|
|
33
|
+
*/
|
|
34
|
+
export function readStackConfigFile(dir: string = cwd()): StackConfigFile | undefined {
|
|
35
|
+
const configPath = resolve(dir, STACK_CONFIG_DIR, STACK_CONFIG_FILE)
|
|
36
|
+
if (!existsSync(configPath)) return undefined
|
|
37
|
+
|
|
38
|
+
let parsed: Record<string, unknown>
|
|
39
|
+
try {
|
|
40
|
+
parsed = JSON.parse(readFileSync(configPath, 'utf8'))
|
|
41
|
+
} catch (error) {
|
|
42
|
+
throw new Error(`Unable to parse ${configPath}`, {cause: error})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {configPath, ...pickIds(parsed)}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Read the Stack ids from `SANITY_ORGANIZATION_ID`, `SANITY_PROJECT_ID`, and `SANITY_BLUEPRINT_STACK_ID`
|
|
50
|
+
* @param env The environment to read. Defaults to `process.env`.
|
|
51
|
+
* @internal
|
|
52
|
+
* @private
|
|
53
|
+
* @category Resolve
|
|
54
|
+
*/
|
|
55
|
+
export function stackIdsFromEnv(env: NodeJS.ProcessEnv = processEnv): StackIds {
|
|
56
|
+
return pickIds({
|
|
57
|
+
organizationId: env.SANITY_ORGANIZATION_ID,
|
|
58
|
+
projectId: env.SANITY_PROJECT_ID,
|
|
59
|
+
stackId: env.SANITY_BLUEPRINT_STACK_ID,
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Resolve the Stack ids and scope, and which source supplied each id
|
|
65
|
+
*
|
|
66
|
+
* Precedence: `overrides`, `env`, `module`, `config`. The scope is the project if known, else the organization.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const config = resolveStackConfig({config: readStackConfigFile()})
|
|
71
|
+
* config.stackId // 'ST-abc123'
|
|
72
|
+
* config.sources.stackId // 'config'
|
|
73
|
+
* ```
|
|
74
|
+
* @internal
|
|
75
|
+
* @private
|
|
76
|
+
* @category Resolve
|
|
77
|
+
*/
|
|
78
|
+
export function resolveStackConfig(sources: StackIdSources = {}): StackConfig {
|
|
79
|
+
const ordered: [ConfigSource, StackIds | null | undefined][] = [
|
|
80
|
+
['overrides', sources.overrides],
|
|
81
|
+
['env', sources.env === undefined ? stackIdsFromEnv() : sources.env],
|
|
82
|
+
['module', sources.module],
|
|
83
|
+
['config', sources.config],
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
const config: StackConfig = {sources: {}}
|
|
87
|
+
for (const [source, values] of ordered) {
|
|
88
|
+
if (!values) continue
|
|
89
|
+
for (const key of ID_KEYS) {
|
|
90
|
+
const value = values[key]
|
|
91
|
+
if (!config[key] && value) {
|
|
92
|
+
config[key] = value
|
|
93
|
+
config.sources[key] = source
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (config.projectId) {
|
|
99
|
+
config.scopeType = 'project'
|
|
100
|
+
config.scopeId = config.projectId
|
|
101
|
+
} else if (config.organizationId) {
|
|
102
|
+
config.scopeType = 'organization'
|
|
103
|
+
config.scopeId = config.organizationId
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return config
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function pickIds(values: Record<string, unknown>): StackIds {
|
|
110
|
+
const ids: StackIds = {}
|
|
111
|
+
for (const key of ID_KEYS) {
|
|
112
|
+
const value = values[key]
|
|
113
|
+
if (typeof value === 'string' && value.length > 0) ids[key] = value
|
|
114
|
+
}
|
|
115
|
+
return ids
|
|
116
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import {env as processEnv} from 'node:process'
|
|
2
|
+
import {byName} from './by-name.js'
|
|
3
|
+
import {readBlueprint} from './load.js'
|
|
4
|
+
import type {DeployedStack, LiveResource, ReadStackOptions, StackInfo} from './types.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The Blueprints API version {@link readStack} uses when none is configured
|
|
8
|
+
* @category Resolve
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_BLUEPRINTS_API_VERSION = 'v2026-06-15'
|
|
11
|
+
|
|
12
|
+
const API_URLS: Record<string, string> = {
|
|
13
|
+
production: 'https://api.sanity.io',
|
|
14
|
+
staging: 'https://api.sanity.work',
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Read a local blueprint and its deployed Stack
|
|
19
|
+
*
|
|
20
|
+
* Adds each deployed resource's `externalId` and `live` values to the declared resource with the same type and name.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import {readStack} from '@sanity/blueprints/resolve'
|
|
25
|
+
*
|
|
26
|
+
* const {values, resourcesByName} = await readStack({token: process.env.SANITY_AUTH_TOKEN})
|
|
27
|
+
* values['production-dataset']?.['name'] // the deployed dataset's name
|
|
28
|
+
* resourcesByName['my-function']?.externalId
|
|
29
|
+
* ```
|
|
30
|
+
* @throws When {@link readBlueprint} throws, the Stack id or scope is unknown, or the API responds with an error
|
|
31
|
+
* @category Resolve
|
|
32
|
+
*/
|
|
33
|
+
export async function readStack(options: ReadStackOptions): Promise<StackInfo> {
|
|
34
|
+
const blueprint = await readBlueprint(options)
|
|
35
|
+
const {stackId, scopeType, scopeId} = blueprint
|
|
36
|
+
|
|
37
|
+
if (!stackId || !scopeType || !scopeId) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`No Stack to read for ${blueprint.path}. Set a Stack id and a project or organization id in ` +
|
|
40
|
+
'.sanity/blueprint.config.json, in defineBlueprint, or in the SANITY_BLUEPRINT_STACK_ID and ' +
|
|
41
|
+
'SANITY_PROJECT_ID or SANITY_ORGANIZATION_ID environment variables.',
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const env = options.env === undefined ? processEnv : options.env
|
|
46
|
+
const apiUrl = (options.apiUrl ?? apiUrlFromEnv(env)).replace(/\/$/, '')
|
|
47
|
+
const apiVersion = options.apiVersion ?? env?.SANITY_BLUEPRINTS_API_VERSION ?? DEFAULT_BLUEPRINTS_API_VERSION
|
|
48
|
+
const fetchFn = options.fetch ?? globalThis.fetch
|
|
49
|
+
|
|
50
|
+
const response = await fetchFn(`${apiUrl}/${apiVersion}/blueprints/stacks/${stackId}`, {
|
|
51
|
+
method: 'GET',
|
|
52
|
+
headers: {
|
|
53
|
+
Accept: 'application/json',
|
|
54
|
+
Authorization: `Bearer ${options.token}`,
|
|
55
|
+
'X-Sanity-Scope-Type': scopeType,
|
|
56
|
+
'X-Sanity-Scope-Id': scopeId,
|
|
57
|
+
'User-Agent': '@sanity/blueprints',
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
throw new Error(`Failed to read Stack ${stackId}: ${response.status} ${await errorMessage(response)}`)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const stack: DeployedStack = await response.json()
|
|
65
|
+
const deployed = new Map(stack.resources.map((resource) => [`${resource.type}:${resource.name}`, resource]))
|
|
66
|
+
|
|
67
|
+
const resources: LiveResource[] = blueprint.resources.map((resource) => {
|
|
68
|
+
const match = deployed.get(`${resource.type}:${resource.name}`)
|
|
69
|
+
if (!match) return resource
|
|
70
|
+
return {
|
|
71
|
+
...resource,
|
|
72
|
+
externalId: match.externalId ?? undefined,
|
|
73
|
+
live: match.resolvedParameters ?? match.parameters,
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
const values: StackInfo['values'] = {}
|
|
78
|
+
for (const resource of resources) {
|
|
79
|
+
if (resource.live) values[resource.name] = resource.live
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {...blueprint, stack, resources, resourcesByName: byName(resources), values}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function apiUrlFromEnv(env: NodeJS.ProcessEnv | null): string {
|
|
86
|
+
const sanityEnv = env?.SANITY_INTERNAL_ENV?.toLowerCase()
|
|
87
|
+
if (!sanityEnv) return API_URLS.production
|
|
88
|
+
// a value that is not a known environment is the origin itself
|
|
89
|
+
return API_URLS[sanityEnv] ?? sanityEnv
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function errorMessage(response: Response): Promise<string> {
|
|
93
|
+
try {
|
|
94
|
+
const body: {message?: unknown} = await response.json()
|
|
95
|
+
if (typeof body.message === 'string') return body.message
|
|
96
|
+
} catch {
|
|
97
|
+
// the body was not JSON
|
|
98
|
+
}
|
|
99
|
+
return response.statusText
|
|
100
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type {Blueprint, BlueprintModule, BlueprintResource, BlueprintsApiConfig} from '../index.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The scope a Stack lives in
|
|
5
|
+
* @category Resolve
|
|
6
|
+
*/
|
|
7
|
+
export type ScopeType = 'organization' | 'project'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Where a resolved id came from, highest precedence first
|
|
11
|
+
*
|
|
12
|
+
* - `overrides`: passed by the caller, for example from command line flags
|
|
13
|
+
* - `env`: the `SANITY_ORGANIZATION_ID`, `SANITY_PROJECT_ID`, and `SANITY_BLUEPRINT_STACK_ID` environment variables
|
|
14
|
+
* - `module`: attached to the blueprint module by `defineBlueprint`
|
|
15
|
+
* - `config`: `.sanity/blueprint.config.json` next to the blueprint file
|
|
16
|
+
* @category Resolve
|
|
17
|
+
*/
|
|
18
|
+
export type ConfigSource = 'overrides' | 'env' | 'module' | 'config'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The ids that identify a Stack
|
|
22
|
+
* @category Resolve
|
|
23
|
+
*/
|
|
24
|
+
export type StackIds = Partial<BlueprintsApiConfig>
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The ids from each source, in the order {@link resolveStackConfig} checks them
|
|
28
|
+
* @internal
|
|
29
|
+
* @private
|
|
30
|
+
* @category Resolve
|
|
31
|
+
*/
|
|
32
|
+
export interface StackIdSources {
|
|
33
|
+
overrides?: StackIds | null
|
|
34
|
+
/** Defaults to the `SANITY_*` environment variables. Pass `null` to skip them. */
|
|
35
|
+
env?: StackIds | null
|
|
36
|
+
module?: StackIds | null
|
|
37
|
+
config?: StackIds | null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The Stack ids and scope, with the source of each id
|
|
42
|
+
* @category Resolve
|
|
43
|
+
*/
|
|
44
|
+
export interface StackConfig extends StackIds {
|
|
45
|
+
/** `project` when a project id is known, else `organization` when an organization id is known */
|
|
46
|
+
scopeType?: ScopeType
|
|
47
|
+
scopeId?: string
|
|
48
|
+
/** Which source supplied each id */
|
|
49
|
+
sources: {[K in keyof BlueprintsApiConfig]?: ConfigSource}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The ids in `.sanity/blueprint.config.json`, and the path of that file
|
|
54
|
+
* @internal
|
|
55
|
+
* @private
|
|
56
|
+
* @category Resolve
|
|
57
|
+
*/
|
|
58
|
+
export interface StackConfigFile extends StackIds {
|
|
59
|
+
configPath: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Imports a blueprint file and returns its default export
|
|
64
|
+
* @category Resolve
|
|
65
|
+
*/
|
|
66
|
+
export type BlueprintLoader = (path: string) => Promise<unknown>
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Options for loading a blueprint file
|
|
70
|
+
* @category Resolve
|
|
71
|
+
*/
|
|
72
|
+
export interface LoadBlueprintFileOptions {
|
|
73
|
+
/** Imports a JS or TS blueprint file in place of the built-in loading, for example to skip the module cache */
|
|
74
|
+
loader?: BlueprintLoader
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A blueprint file as loaded from disk
|
|
79
|
+
* @category Resolve
|
|
80
|
+
*/
|
|
81
|
+
export interface LoadedBlueprintFile {
|
|
82
|
+
/** The blueprint document the module returned or the JSON file held */
|
|
83
|
+
blueprint: Blueprint
|
|
84
|
+
/** The default export of a JS or TS file. Absent when the file was JSON. */
|
|
85
|
+
module?: BlueprintModule
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Options for {@link readBlueprint}
|
|
90
|
+
* @category Resolve
|
|
91
|
+
*/
|
|
92
|
+
export interface ReadBlueprintOptions extends LoadBlueprintFileOptions {
|
|
93
|
+
/** A blueprint file or directory. Defaults to the current working directory. */
|
|
94
|
+
path?: string
|
|
95
|
+
/** Ids that win over every other source */
|
|
96
|
+
overrides?: StackIds | null
|
|
97
|
+
/** The environment to read. Defaults to `process.env`. Pass `null` to skip environment variables. */
|
|
98
|
+
env?: NodeJS.ProcessEnv | null
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* What a blueprint declares, and the ids that say which Stack it deploys to
|
|
103
|
+
* @category Resolve
|
|
104
|
+
*/
|
|
105
|
+
export interface BlueprintInfo extends StackConfig, LoadedBlueprintFile {
|
|
106
|
+
/** The absolute path of the blueprint file */
|
|
107
|
+
path: string
|
|
108
|
+
/** `blueprint.resources`, or an empty list */
|
|
109
|
+
resources: BlueprintResource[]
|
|
110
|
+
/** `resources` keyed by name. A later duplicate name wins. */
|
|
111
|
+
resourcesByName: Record<string, BlueprintResource>
|
|
112
|
+
/** The `.sanity/blueprint.config.json` that was read, when one exists */
|
|
113
|
+
configPath?: string
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A resource as the Blueprints API returns it
|
|
118
|
+
* @category Resolve
|
|
119
|
+
*/
|
|
120
|
+
export interface DeployedResource {
|
|
121
|
+
id: string
|
|
122
|
+
name: string
|
|
123
|
+
type: string
|
|
124
|
+
externalId?: string | null
|
|
125
|
+
parameters?: Record<string, unknown>
|
|
126
|
+
/** Parameters with references to other resources resolved */
|
|
127
|
+
resolvedParameters?: Record<string, unknown>
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A Stack as the Blueprints API returns it
|
|
132
|
+
* @category Resolve
|
|
133
|
+
*/
|
|
134
|
+
export interface DeployedStack {
|
|
135
|
+
id: string
|
|
136
|
+
name: string
|
|
137
|
+
scopeType: ScopeType
|
|
138
|
+
scopeId: string
|
|
139
|
+
defaultProjectId?: string | null
|
|
140
|
+
resources: DeployedResource[]
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A declared resource with its deployed values, when the Stack has a resource of the same type and name
|
|
145
|
+
* @category Resolve
|
|
146
|
+
*/
|
|
147
|
+
export type LiveResource = BlueprintResource & {
|
|
148
|
+
/** The id of the resource in the service that owns it */
|
|
149
|
+
externalId?: string
|
|
150
|
+
/** The deployed parameters, with references resolved when the API has them */
|
|
151
|
+
live?: Record<string, unknown>
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A blueprint with the values its deployed Stack holds
|
|
156
|
+
* @category Resolve
|
|
157
|
+
*/
|
|
158
|
+
export interface StackInfo extends BlueprintInfo {
|
|
159
|
+
stack: DeployedStack
|
|
160
|
+
resources: LiveResource[]
|
|
161
|
+
resourcesByName: Record<string, LiveResource>
|
|
162
|
+
/** `live` parameters by resource name, for the resources the Stack has deployed */
|
|
163
|
+
values: Record<string, Record<string, unknown>>
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Options for {@link readStack}
|
|
168
|
+
* @category Resolve
|
|
169
|
+
*/
|
|
170
|
+
export interface ReadStackOptions extends ReadBlueprintOptions {
|
|
171
|
+
/** A Sanity auth token */
|
|
172
|
+
token: string
|
|
173
|
+
/** Defaults to the global `fetch` */
|
|
174
|
+
fetch?: typeof fetch
|
|
175
|
+
/** The Sanity API origin. Defaults from `SANITY_INTERNAL_ENV`. */
|
|
176
|
+
apiUrl?: string
|
|
177
|
+
/** The Blueprints API version. Defaults to `SANITY_BLUEPRINTS_API_VERSION` or {@link DEFAULT_BLUEPRINTS_API_VERSION}. */
|
|
178
|
+
apiVersion?: string
|
|
179
|
+
}
|
|
@@ -63,15 +63,10 @@ interface QueueConfig {
|
|
|
63
63
|
*/
|
|
64
64
|
concurrency?: number
|
|
65
65
|
/**
|
|
66
|
-
* Debounce
|
|
67
|
-
*
|
|
68
|
-
*/
|
|
69
|
-
debounce?: number
|
|
70
|
-
/**
|
|
71
|
-
* Path used to group debounced events, e.g. 'document._id'
|
|
72
|
-
* @todo: not implemented - and should this always be provided with debounce window?
|
|
66
|
+
* Debounce configuration, with any durations already resolved to seconds.
|
|
67
|
+
* Definers accept the wider {@link DebounceInput} and parse it into this shape.
|
|
73
68
|
*/
|
|
74
|
-
|
|
69
|
+
debounce?: DebounceConfig
|
|
75
70
|
/**
|
|
76
71
|
* Whether to place messages that failed processing into a Dead Letter Queue.
|
|
77
72
|
* @todo: not implemented
|
|
@@ -87,6 +82,56 @@ interface QueueConfig {
|
|
|
87
82
|
fifo?: boolean
|
|
88
83
|
}
|
|
89
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Configuration used when debouncing Queue or Workflow function types.
|
|
87
|
+
* @category Functions Types
|
|
88
|
+
* @alpha
|
|
89
|
+
* @hidden
|
|
90
|
+
*/
|
|
91
|
+
export interface DebounceConfig {
|
|
92
|
+
/**
|
|
93
|
+
* Debounce window in seconds
|
|
94
|
+
*/
|
|
95
|
+
window: number
|
|
96
|
+
/**
|
|
97
|
+
* Maximum time in seconds to extend a debounce window from the first event
|
|
98
|
+
*/
|
|
99
|
+
maxWindow?: number
|
|
100
|
+
/**
|
|
101
|
+
* Dotted path into {event, context} used to debounce events
|
|
102
|
+
*/
|
|
103
|
+
key?: string
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Debounce configuration as accepted by a function definer, before durations are parsed.
|
|
108
|
+
* @category Functions Types
|
|
109
|
+
* @alpha
|
|
110
|
+
* @hidden
|
|
111
|
+
*/
|
|
112
|
+
export interface DebounceConfigInput extends Omit<DebounceConfig, 'window' | 'maxWindow'> {
|
|
113
|
+
/**
|
|
114
|
+
* Debounce window
|
|
115
|
+
* A number is read as seconds, a string is parsed as a duration, e.g. `'5 minutes'`.
|
|
116
|
+
*/
|
|
117
|
+
window: string | number
|
|
118
|
+
/**
|
|
119
|
+
* Maximum time to extend a debounce window from the first event.
|
|
120
|
+
* Must be greater than `window`.
|
|
121
|
+
* A number is read as seconds, a string is parsed as a duration, e.g. `'1 hour'`.
|
|
122
|
+
*/
|
|
123
|
+
maxWindow?: string | number
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Debounce configuration accepted by a function definer, either as a bare duration
|
|
128
|
+
* used as the window, or as a full config.
|
|
129
|
+
* @category Functions Types
|
|
130
|
+
* @alpha
|
|
131
|
+
* @hidden
|
|
132
|
+
*/
|
|
133
|
+
export type DebounceInput = string | number | DebounceConfigInput
|
|
134
|
+
|
|
90
135
|
/**
|
|
91
136
|
* Base function resource with common properties for all function types that can belong to projects.
|
|
92
137
|
* @category Functions Types
|
|
@@ -167,6 +212,10 @@ export interface BlueprintPubSubFunctionResource extends BlueprintBaseFunctionRe
|
|
|
167
212
|
export interface BlueprintDurableFunctionResource extends BlueprintBaseFunctionResource, QueueConfig {
|
|
168
213
|
type: 'sanity.function.durable'
|
|
169
214
|
event?: BlueprintFunctionResourceContentLakeEvent
|
|
215
|
+
/**
|
|
216
|
+
* Total durable execution timeout in seconds.
|
|
217
|
+
*/
|
|
218
|
+
durableTimeout?: number
|
|
170
219
|
}
|
|
171
220
|
|
|
172
221
|
// --- Function Config (Input) Types: : `define*Function` method parameter types ---
|
|
@@ -258,17 +307,19 @@ export type BlueprintSyncTagInvalidateFunctionConfig = Omit<BlueprintSyncTagInva
|
|
|
258
307
|
* @category Functions Types
|
|
259
308
|
* @interface
|
|
260
309
|
*/
|
|
261
|
-
export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource, 'type' | 'src' | 'event'> &
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
src?: string
|
|
310
|
+
export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource, 'type' | 'src' | 'event' | 'debounce'> & {
|
|
311
|
+
/**
|
|
312
|
+
* Path to the function source code
|
|
313
|
+
* @defaultValue `functions/${name}`
|
|
314
|
+
*/
|
|
315
|
+
src?: string
|
|
268
316
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
317
|
+
/** Optional event configuration that triggers the queue function */
|
|
318
|
+
event?: BlueprintFunctionResourceContentLakeEvent
|
|
319
|
+
|
|
320
|
+
/** Optional debounce configuration, as a duration or a full config */
|
|
321
|
+
debounce?: DebounceInput
|
|
322
|
+
}
|
|
272
323
|
|
|
273
324
|
/**
|
|
274
325
|
* Configuration for defining an event function.
|
|
@@ -292,7 +343,7 @@ export type BlueprintPubSubFunctionConfig = Omit<BlueprintPubSubFunctionResource
|
|
|
292
343
|
* @category Functions Types
|
|
293
344
|
* @interface
|
|
294
345
|
*/
|
|
295
|
-
export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'type' | 'src' | 'event'> & {
|
|
346
|
+
export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'type' | 'src' | 'event' | 'durableTimeout' | 'debounce'> & {
|
|
296
347
|
/**
|
|
297
348
|
* Path to the function source code
|
|
298
349
|
* @defaultValue `functions/${name}`
|
|
@@ -304,7 +355,11 @@ export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'typ
|
|
|
304
355
|
event?: BlueprintFunctionResourceContentLakeEvent
|
|
305
356
|
/**
|
|
306
357
|
* Total durable execution timeout in seconds.
|
|
307
|
-
* @defaultValue 86_400
|
|
358
|
+
* @defaultValue 86_400 (24 hours)
|
|
308
359
|
*/
|
|
309
|
-
durableTimeout?: number
|
|
360
|
+
durableTimeout?: string | number
|
|
361
|
+
/**
|
|
362
|
+
* Optional debounce configuration, as a duration or a full config
|
|
363
|
+
*/
|
|
364
|
+
debounce?: DebounceInput
|
|
310
365
|
}
|
|
@@ -62,6 +62,9 @@ export interface BlueprintMediaLibraryConfigResource extends BlueprintInstallati
|
|
|
62
62
|
|
|
63
63
|
/** The custom fields the Media Library config exposes. */
|
|
64
64
|
fields: BlueprintMediaLibraryField[]
|
|
65
|
+
|
|
66
|
+
/** The local version of `sanity` at deployment. Populated during deployment. */
|
|
67
|
+
version?: string
|
|
65
68
|
}
|
|
66
69
|
|
|
67
70
|
/**
|
package/src/types/resources.ts
CHANGED
|
@@ -73,20 +73,26 @@ export interface BlueprintResourceLifecycle {
|
|
|
73
73
|
ownershipAction?: BlueprintOwnershipAction
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
|
-
* Declares a dependency on another resource in the blueprint.
|
|
77
|
-
* The referenced
|
|
76
|
+
* Declares a dependency on another resource or list of resources in the blueprint.
|
|
77
|
+
* The referenced resources will be deployed before this one.
|
|
78
78
|
*
|
|
79
|
-
* The
|
|
80
|
-
* the
|
|
79
|
+
* The values must be resource references starting with `$.resources.` followed by the name of
|
|
80
|
+
* the resources this resource depends on.
|
|
81
81
|
*
|
|
82
82
|
* @example
|
|
83
83
|
* ```ts
|
|
84
|
+
* // single dependency
|
|
84
85
|
* lifecycle: {
|
|
85
86
|
* dependsOn: '$.resources.my-dataset',
|
|
86
87
|
* }
|
|
88
|
+
*
|
|
89
|
+
* // multiple dependencies
|
|
90
|
+
* lifecycle: {
|
|
91
|
+
* dependsOn: ['$.resources.my-project', '$.resources.my-dataset'],
|
|
92
|
+
* }
|
|
87
93
|
* ```
|
|
88
94
|
*/
|
|
89
|
-
dependsOn?: string
|
|
95
|
+
dependsOn?: string | string[]
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
/**
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type {BlueprintError} from '../types/errors.js'
|
|
2
|
+
import type {DebounceConfig, DebounceInput} from '../types/functions/index.js'
|
|
3
|
+
import {parseDuration} from './parse-duration.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes the debounce config accepted by a definer into the shape held by a resource,
|
|
7
|
+
* parsing any duration into seconds. A bare duration is shorthand for `{window: <duration>}`.
|
|
8
|
+
* @param debounce The debounce config or duration to normalize
|
|
9
|
+
* @internal
|
|
10
|
+
* @returns The debounce config, with `window` and `maxWindow` in seconds
|
|
11
|
+
*/
|
|
12
|
+
export function createDebounceObject(debounce: DebounceInput): DebounceConfig {
|
|
13
|
+
if (typeof debounce !== 'object') {
|
|
14
|
+
return {window: parseDuration(debounce, 's')}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const {window, maxWindow, key} = debounce
|
|
18
|
+
|
|
19
|
+
// `window` is required, so there is nothing sensible to parse without it
|
|
20
|
+
if (window === undefined) {
|
|
21
|
+
throw new Error('Invalid debounce config: `window` must be provided')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
window: parseDuration(window, 's'),
|
|
26
|
+
...(maxWindow !== undefined && {maxWindow: parseDuration(maxWindow, 's')}),
|
|
27
|
+
...(key !== undefined && {key}),
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Resolves a debounce duration, given either as a number of seconds or as a duration string, to seconds.
|
|
33
|
+
* @param field The name of the field being resolved, used in error messages
|
|
34
|
+
* @param value The value to resolve
|
|
35
|
+
* @returns The value in seconds, or the errors that prevented it from being resolved
|
|
36
|
+
*/
|
|
37
|
+
export function resolveDebounceDuration(field: string, value: unknown): {seconds?: number; errors: BlueprintError[]} {
|
|
38
|
+
if (typeof value === 'number') {
|
|
39
|
+
return Number.isFinite(value)
|
|
40
|
+
? {seconds: value, errors: []}
|
|
41
|
+
: {errors: [{type: 'invalid_value', message: `\`${field}\` must be a valid duration`}]}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (typeof value === 'string') {
|
|
45
|
+
try {
|
|
46
|
+
return {seconds: parseDuration(value, 's'), errors: []}
|
|
47
|
+
} catch {
|
|
48
|
+
return {errors: [{type: 'invalid_value', message: `\`${field}\` must be a valid duration`}]}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {errors: [{type: 'invalid_type', message: `\`${field}\` must be a number of seconds or a duration string`}]}
|
|
53
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import parse from 'parse-duration'
|
|
2
|
+
|
|
3
|
+
type DurationUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'mo' | 'y'
|
|
4
|
+
|
|
5
|
+
const YEAR_IN_SEC = 31_536_000
|
|
6
|
+
const YEAR_IN_MS = YEAR_IN_SEC * 1000
|
|
7
|
+
const units = parse.unit as Record<string, number>
|
|
8
|
+
|
|
9
|
+
// Extend the units object because of the calculations of parse-duration of year to secs - `31557600000`
|
|
10
|
+
Object.assign(units, {
|
|
11
|
+
year: YEAR_IN_MS,
|
|
12
|
+
yr: YEAR_IN_MS,
|
|
13
|
+
y: YEAR_IN_MS,
|
|
14
|
+
|
|
15
|
+
month: YEAR_IN_MS / 12,
|
|
16
|
+
mth: YEAR_IN_MS / 12,
|
|
17
|
+
mo: YEAR_IN_MS / 12,
|
|
18
|
+
|
|
19
|
+
week: YEAR_IN_MS / 52,
|
|
20
|
+
wk: YEAR_IN_MS / 52,
|
|
21
|
+
w: YEAR_IN_MS / 52,
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Small parse-duration wrapper for string or number
|
|
26
|
+
* @param duration
|
|
27
|
+
* @param unit
|
|
28
|
+
* @return parsed number in the specified unit
|
|
29
|
+
*/
|
|
30
|
+
export function parseDuration(duration: string | number, unit: DurationUnit = 's'): number {
|
|
31
|
+
const parsed = typeof duration === 'number' ? duration : parse(duration, unit)
|
|
32
|
+
|
|
33
|
+
if (parsed === null || !Number.isFinite(parsed)) {
|
|
34
|
+
throw new Error(`Invalid duration: ${duration}`)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return parsed
|
|
38
|
+
}
|