@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,23 @@
|
|
|
1
|
+
import type { ReadStackOptions, StackInfo } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The Blueprints API version {@link readStack} uses when none is configured
|
|
4
|
+
* @category Resolve
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_BLUEPRINTS_API_VERSION = "v2026-06-15";
|
|
7
|
+
/**
|
|
8
|
+
* Read a local blueprint and its deployed Stack
|
|
9
|
+
*
|
|
10
|
+
* Adds each deployed resource's `externalId` and `live` values to the declared resource with the same type and name.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {readStack} from '@sanity/blueprints/resolve'
|
|
15
|
+
*
|
|
16
|
+
* const {values, resourcesByName} = await readStack({token: process.env.SANITY_AUTH_TOKEN})
|
|
17
|
+
* values['production-dataset']?.['name'] // the deployed dataset's name
|
|
18
|
+
* resourcesByName['my-function']?.externalId
|
|
19
|
+
* ```
|
|
20
|
+
* @throws When {@link readBlueprint} throws, the Stack id or scope is unknown, or the API responds with an error
|
|
21
|
+
* @category Resolve
|
|
22
|
+
*/
|
|
23
|
+
export declare function readStack(options: ReadStackOptions): Promise<StackInfo>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { env as processEnv } from 'node:process';
|
|
2
|
+
import { byName } from './by-name.js';
|
|
3
|
+
import { readBlueprint } from './load.js';
|
|
4
|
+
/**
|
|
5
|
+
* The Blueprints API version {@link readStack} uses when none is configured
|
|
6
|
+
* @category Resolve
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_BLUEPRINTS_API_VERSION = 'v2026-06-15';
|
|
9
|
+
const API_URLS = {
|
|
10
|
+
production: 'https://api.sanity.io',
|
|
11
|
+
staging: 'https://api.sanity.work',
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Read a local blueprint and its deployed Stack
|
|
15
|
+
*
|
|
16
|
+
* Adds each deployed resource's `externalId` and `live` values to the declared resource with the same type and name.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import {readStack} from '@sanity/blueprints/resolve'
|
|
21
|
+
*
|
|
22
|
+
* const {values, resourcesByName} = await readStack({token: process.env.SANITY_AUTH_TOKEN})
|
|
23
|
+
* values['production-dataset']?.['name'] // the deployed dataset's name
|
|
24
|
+
* resourcesByName['my-function']?.externalId
|
|
25
|
+
* ```
|
|
26
|
+
* @throws When {@link readBlueprint} throws, the Stack id or scope is unknown, or the API responds with an error
|
|
27
|
+
* @category Resolve
|
|
28
|
+
*/
|
|
29
|
+
export async function readStack(options) {
|
|
30
|
+
const blueprint = await readBlueprint(options);
|
|
31
|
+
const { stackId, scopeType, scopeId } = blueprint;
|
|
32
|
+
if (!stackId || !scopeType || !scopeId) {
|
|
33
|
+
throw new Error(`No Stack to read for ${blueprint.path}. Set a Stack id and a project or organization id in ` +
|
|
34
|
+
'.sanity/blueprint.config.json, in defineBlueprint, or in the SANITY_BLUEPRINT_STACK_ID and ' +
|
|
35
|
+
'SANITY_PROJECT_ID or SANITY_ORGANIZATION_ID environment variables.');
|
|
36
|
+
}
|
|
37
|
+
const env = options.env === undefined ? processEnv : options.env;
|
|
38
|
+
const apiUrl = (options.apiUrl ?? apiUrlFromEnv(env)).replace(/\/$/, '');
|
|
39
|
+
const apiVersion = options.apiVersion ?? env?.SANITY_BLUEPRINTS_API_VERSION ?? DEFAULT_BLUEPRINTS_API_VERSION;
|
|
40
|
+
const fetchFn = options.fetch ?? globalThis.fetch;
|
|
41
|
+
const response = await fetchFn(`${apiUrl}/${apiVersion}/blueprints/stacks/${stackId}`, {
|
|
42
|
+
method: 'GET',
|
|
43
|
+
headers: {
|
|
44
|
+
Accept: 'application/json',
|
|
45
|
+
Authorization: `Bearer ${options.token}`,
|
|
46
|
+
'X-Sanity-Scope-Type': scopeType,
|
|
47
|
+
'X-Sanity-Scope-Id': scopeId,
|
|
48
|
+
'User-Agent': '@sanity/blueprints',
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
throw new Error(`Failed to read Stack ${stackId}: ${response.status} ${await errorMessage(response)}`);
|
|
53
|
+
}
|
|
54
|
+
const stack = await response.json();
|
|
55
|
+
const deployed = new Map(stack.resources.map((resource) => [`${resource.type}:${resource.name}`, resource]));
|
|
56
|
+
const resources = blueprint.resources.map((resource) => {
|
|
57
|
+
const match = deployed.get(`${resource.type}:${resource.name}`);
|
|
58
|
+
if (!match)
|
|
59
|
+
return resource;
|
|
60
|
+
return {
|
|
61
|
+
...resource,
|
|
62
|
+
externalId: match.externalId ?? undefined,
|
|
63
|
+
live: match.resolvedParameters ?? match.parameters,
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
const values = {};
|
|
67
|
+
for (const resource of resources) {
|
|
68
|
+
if (resource.live)
|
|
69
|
+
values[resource.name] = resource.live;
|
|
70
|
+
}
|
|
71
|
+
return { ...blueprint, stack, resources, resourcesByName: byName(resources), values };
|
|
72
|
+
}
|
|
73
|
+
function apiUrlFromEnv(env) {
|
|
74
|
+
const sanityEnv = env?.SANITY_INTERNAL_ENV?.toLowerCase();
|
|
75
|
+
if (!sanityEnv)
|
|
76
|
+
return API_URLS.production;
|
|
77
|
+
// a value that is not a known environment is the origin itself
|
|
78
|
+
return API_URLS[sanityEnv] ?? sanityEnv;
|
|
79
|
+
}
|
|
80
|
+
async function errorMessage(response) {
|
|
81
|
+
try {
|
|
82
|
+
const body = await response.json();
|
|
83
|
+
if (typeof body.message === 'string')
|
|
84
|
+
return body.message;
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// the body was not JSON
|
|
88
|
+
}
|
|
89
|
+
return response.statusText;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=stack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stack.js","sourceRoot":"","sources":["../../src/resolve/stack.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { Blueprint, BlueprintModule, BlueprintResource, BlueprintsApiConfig } from '../index.js';
|
|
2
|
+
/**
|
|
3
|
+
* The scope a Stack lives in
|
|
4
|
+
* @category Resolve
|
|
5
|
+
*/
|
|
6
|
+
export type ScopeType = 'organization' | 'project';
|
|
7
|
+
/**
|
|
8
|
+
* Where a resolved id came from, highest precedence first
|
|
9
|
+
*
|
|
10
|
+
* - `overrides`: passed by the caller, for example from command line flags
|
|
11
|
+
* - `env`: the `SANITY_ORGANIZATION_ID`, `SANITY_PROJECT_ID`, and `SANITY_BLUEPRINT_STACK_ID` environment variables
|
|
12
|
+
* - `module`: attached to the blueprint module by `defineBlueprint`
|
|
13
|
+
* - `config`: `.sanity/blueprint.config.json` next to the blueprint file
|
|
14
|
+
* @category Resolve
|
|
15
|
+
*/
|
|
16
|
+
export type ConfigSource = 'overrides' | 'env' | 'module' | 'config';
|
|
17
|
+
/**
|
|
18
|
+
* The ids that identify a Stack
|
|
19
|
+
* @category Resolve
|
|
20
|
+
*/
|
|
21
|
+
export type StackIds = Partial<BlueprintsApiConfig>;
|
|
22
|
+
/**
|
|
23
|
+
* The ids from each source, in the order {@link resolveStackConfig} checks them
|
|
24
|
+
* @internal
|
|
25
|
+
* @private
|
|
26
|
+
* @category Resolve
|
|
27
|
+
*/
|
|
28
|
+
export interface StackIdSources {
|
|
29
|
+
overrides?: StackIds | null;
|
|
30
|
+
/** Defaults to the `SANITY_*` environment variables. Pass `null` to skip them. */
|
|
31
|
+
env?: StackIds | null;
|
|
32
|
+
module?: StackIds | null;
|
|
33
|
+
config?: StackIds | null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The Stack ids and scope, with the source of each id
|
|
37
|
+
* @category Resolve
|
|
38
|
+
*/
|
|
39
|
+
export interface StackConfig extends StackIds {
|
|
40
|
+
/** `project` when a project id is known, else `organization` when an organization id is known */
|
|
41
|
+
scopeType?: ScopeType;
|
|
42
|
+
scopeId?: string;
|
|
43
|
+
/** Which source supplied each id */
|
|
44
|
+
sources: {
|
|
45
|
+
[K in keyof BlueprintsApiConfig]?: ConfigSource;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The ids in `.sanity/blueprint.config.json`, and the path of that file
|
|
50
|
+
* @internal
|
|
51
|
+
* @private
|
|
52
|
+
* @category Resolve
|
|
53
|
+
*/
|
|
54
|
+
export interface StackConfigFile extends StackIds {
|
|
55
|
+
configPath: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Imports a blueprint file and returns its default export
|
|
59
|
+
* @category Resolve
|
|
60
|
+
*/
|
|
61
|
+
export type BlueprintLoader = (path: string) => Promise<unknown>;
|
|
62
|
+
/**
|
|
63
|
+
* Options for loading a blueprint file
|
|
64
|
+
* @category Resolve
|
|
65
|
+
*/
|
|
66
|
+
export interface LoadBlueprintFileOptions {
|
|
67
|
+
/** Imports a JS or TS blueprint file in place of the built-in loading, for example to skip the module cache */
|
|
68
|
+
loader?: BlueprintLoader;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A blueprint file as loaded from disk
|
|
72
|
+
* @category Resolve
|
|
73
|
+
*/
|
|
74
|
+
export interface LoadedBlueprintFile {
|
|
75
|
+
/** The blueprint document the module returned or the JSON file held */
|
|
76
|
+
blueprint: Blueprint;
|
|
77
|
+
/** The default export of a JS or TS file. Absent when the file was JSON. */
|
|
78
|
+
module?: BlueprintModule;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Options for {@link readBlueprint}
|
|
82
|
+
* @category Resolve
|
|
83
|
+
*/
|
|
84
|
+
export interface ReadBlueprintOptions extends LoadBlueprintFileOptions {
|
|
85
|
+
/** A blueprint file or directory. Defaults to the current working directory. */
|
|
86
|
+
path?: string;
|
|
87
|
+
/** Ids that win over every other source */
|
|
88
|
+
overrides?: StackIds | null;
|
|
89
|
+
/** The environment to read. Defaults to `process.env`. Pass `null` to skip environment variables. */
|
|
90
|
+
env?: NodeJS.ProcessEnv | null;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* What a blueprint declares, and the ids that say which Stack it deploys to
|
|
94
|
+
* @category Resolve
|
|
95
|
+
*/
|
|
96
|
+
export interface BlueprintInfo extends StackConfig, LoadedBlueprintFile {
|
|
97
|
+
/** The absolute path of the blueprint file */
|
|
98
|
+
path: string;
|
|
99
|
+
/** `blueprint.resources`, or an empty list */
|
|
100
|
+
resources: BlueprintResource[];
|
|
101
|
+
/** `resources` keyed by name. A later duplicate name wins. */
|
|
102
|
+
resourcesByName: Record<string, BlueprintResource>;
|
|
103
|
+
/** The `.sanity/blueprint.config.json` that was read, when one exists */
|
|
104
|
+
configPath?: string;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* A resource as the Blueprints API returns it
|
|
108
|
+
* @category Resolve
|
|
109
|
+
*/
|
|
110
|
+
export interface DeployedResource {
|
|
111
|
+
id: string;
|
|
112
|
+
name: string;
|
|
113
|
+
type: string;
|
|
114
|
+
externalId?: string | null;
|
|
115
|
+
parameters?: Record<string, unknown>;
|
|
116
|
+
/** Parameters with references to other resources resolved */
|
|
117
|
+
resolvedParameters?: Record<string, unknown>;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* A Stack as the Blueprints API returns it
|
|
121
|
+
* @category Resolve
|
|
122
|
+
*/
|
|
123
|
+
export interface DeployedStack {
|
|
124
|
+
id: string;
|
|
125
|
+
name: string;
|
|
126
|
+
scopeType: ScopeType;
|
|
127
|
+
scopeId: string;
|
|
128
|
+
defaultProjectId?: string | null;
|
|
129
|
+
resources: DeployedResource[];
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* A declared resource with its deployed values, when the Stack has a resource of the same type and name
|
|
133
|
+
* @category Resolve
|
|
134
|
+
*/
|
|
135
|
+
export type LiveResource = BlueprintResource & {
|
|
136
|
+
/** The id of the resource in the service that owns it */
|
|
137
|
+
externalId?: string;
|
|
138
|
+
/** The deployed parameters, with references resolved when the API has them */
|
|
139
|
+
live?: Record<string, unknown>;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* A blueprint with the values its deployed Stack holds
|
|
143
|
+
* @category Resolve
|
|
144
|
+
*/
|
|
145
|
+
export interface StackInfo extends BlueprintInfo {
|
|
146
|
+
stack: DeployedStack;
|
|
147
|
+
resources: LiveResource[];
|
|
148
|
+
resourcesByName: Record<string, LiveResource>;
|
|
149
|
+
/** `live` parameters by resource name, for the resources the Stack has deployed */
|
|
150
|
+
values: Record<string, Record<string, unknown>>;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Options for {@link readStack}
|
|
154
|
+
* @category Resolve
|
|
155
|
+
*/
|
|
156
|
+
export interface ReadStackOptions extends ReadBlueprintOptions {
|
|
157
|
+
/** A Sanity auth token */
|
|
158
|
+
token: string;
|
|
159
|
+
/** Defaults to the global `fetch` */
|
|
160
|
+
fetch?: typeof fetch;
|
|
161
|
+
/** The Sanity API origin. Defaults from `SANITY_INTERNAL_ENV`. */
|
|
162
|
+
apiUrl?: string;
|
|
163
|
+
/** The Blueprints API version. Defaults to `SANITY_BLUEPRINTS_API_VERSION` or {@link DEFAULT_BLUEPRINTS_API_VERSION}. */
|
|
164
|
+
apiVersion?: string;
|
|
165
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/resolve/types.ts"],"names":[],"mappings":""}
|
|
@@ -49,15 +49,10 @@ interface QueueConfig {
|
|
|
49
49
|
*/
|
|
50
50
|
concurrency?: number;
|
|
51
51
|
/**
|
|
52
|
-
* Debounce
|
|
53
|
-
*
|
|
54
|
-
*/
|
|
55
|
-
debounce?: number;
|
|
56
|
-
/**
|
|
57
|
-
* Path used to group debounced events, e.g. 'document._id'
|
|
58
|
-
* @todo: not implemented - and should this always be provided with debounce window?
|
|
52
|
+
* Debounce configuration, with any durations already resolved to seconds.
|
|
53
|
+
* Definers accept the wider {@link DebounceInput} and parse it into this shape.
|
|
59
54
|
*/
|
|
60
|
-
|
|
55
|
+
debounce?: DebounceConfig;
|
|
61
56
|
/**
|
|
62
57
|
* Whether to place messages that failed processing into a Dead Letter Queue.
|
|
63
58
|
* @todo: not implemented
|
|
@@ -72,6 +67,53 @@ interface QueueConfig {
|
|
|
72
67
|
*/
|
|
73
68
|
fifo?: boolean;
|
|
74
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Configuration used when debouncing Queue or Workflow function types.
|
|
72
|
+
* @category Functions Types
|
|
73
|
+
* @alpha
|
|
74
|
+
* @hidden
|
|
75
|
+
*/
|
|
76
|
+
export interface DebounceConfig {
|
|
77
|
+
/**
|
|
78
|
+
* Debounce window in seconds
|
|
79
|
+
*/
|
|
80
|
+
window: number;
|
|
81
|
+
/**
|
|
82
|
+
* Maximum time in seconds to extend a debounce window from the first event
|
|
83
|
+
*/
|
|
84
|
+
maxWindow?: number;
|
|
85
|
+
/**
|
|
86
|
+
* Dotted path into {event, context} used to debounce events
|
|
87
|
+
*/
|
|
88
|
+
key?: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Debounce configuration as accepted by a function definer, before durations are parsed.
|
|
92
|
+
* @category Functions Types
|
|
93
|
+
* @alpha
|
|
94
|
+
* @hidden
|
|
95
|
+
*/
|
|
96
|
+
export interface DebounceConfigInput extends Omit<DebounceConfig, 'window' | 'maxWindow'> {
|
|
97
|
+
/**
|
|
98
|
+
* Debounce window
|
|
99
|
+
* A number is read as seconds, a string is parsed as a duration, e.g. `'5 minutes'`.
|
|
100
|
+
*/
|
|
101
|
+
window: string | number;
|
|
102
|
+
/**
|
|
103
|
+
* Maximum time to extend a debounce window from the first event.
|
|
104
|
+
* Must be greater than `window`.
|
|
105
|
+
* A number is read as seconds, a string is parsed as a duration, e.g. `'1 hour'`.
|
|
106
|
+
*/
|
|
107
|
+
maxWindow?: string | number;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Debounce configuration accepted by a function definer, either as a bare duration
|
|
111
|
+
* used as the window, or as a full config.
|
|
112
|
+
* @category Functions Types
|
|
113
|
+
* @alpha
|
|
114
|
+
* @hidden
|
|
115
|
+
*/
|
|
116
|
+
export type DebounceInput = string | number | DebounceConfigInput;
|
|
75
117
|
/**
|
|
76
118
|
* Base function resource with common properties for all function types that can belong to projects.
|
|
77
119
|
* @category Functions Types
|
|
@@ -145,6 +187,10 @@ export interface BlueprintPubSubFunctionResource extends BlueprintBaseFunctionRe
|
|
|
145
187
|
export interface BlueprintDurableFunctionResource extends BlueprintBaseFunctionResource, QueueConfig {
|
|
146
188
|
type: 'sanity.function.durable';
|
|
147
189
|
event?: BlueprintFunctionResourceContentLakeEvent;
|
|
190
|
+
/**
|
|
191
|
+
* Total durable execution timeout in seconds.
|
|
192
|
+
*/
|
|
193
|
+
durableTimeout?: number;
|
|
148
194
|
}
|
|
149
195
|
/**
|
|
150
196
|
* Configuration for defining a base function.
|
|
@@ -228,7 +274,7 @@ export type BlueprintSyncTagInvalidateFunctionConfig = Omit<BlueprintSyncTagInva
|
|
|
228
274
|
* @category Functions Types
|
|
229
275
|
* @interface
|
|
230
276
|
*/
|
|
231
|
-
export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource, 'type' | 'src' | 'event'
|
|
277
|
+
export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource, 'type' | 'src' | 'event' | 'debounce'> & {
|
|
232
278
|
/**
|
|
233
279
|
* Path to the function source code
|
|
234
280
|
* @defaultValue `functions/${name}`
|
|
@@ -236,6 +282,8 @@ export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource,
|
|
|
236
282
|
src?: string;
|
|
237
283
|
/** Optional event configuration that triggers the queue function */
|
|
238
284
|
event?: BlueprintFunctionResourceContentLakeEvent;
|
|
285
|
+
/** Optional debounce configuration, as a duration or a full config */
|
|
286
|
+
debounce?: DebounceInput;
|
|
239
287
|
};
|
|
240
288
|
/**
|
|
241
289
|
* Configuration for defining an event function.
|
|
@@ -258,7 +306,7 @@ export type BlueprintPubSubFunctionConfig = Omit<BlueprintPubSubFunctionResource
|
|
|
258
306
|
* @category Functions Types
|
|
259
307
|
* @interface
|
|
260
308
|
*/
|
|
261
|
-
export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'type' | 'src' | 'event'> & {
|
|
309
|
+
export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'type' | 'src' | 'event' | 'durableTimeout' | 'debounce'> & {
|
|
262
310
|
/**
|
|
263
311
|
* Path to the function source code
|
|
264
312
|
* @defaultValue `functions/${name}`
|
|
@@ -270,7 +318,11 @@ export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'typ
|
|
|
270
318
|
event?: BlueprintFunctionResourceContentLakeEvent;
|
|
271
319
|
/**
|
|
272
320
|
* Total durable execution timeout in seconds.
|
|
273
|
-
* @defaultValue 86_400
|
|
321
|
+
* @defaultValue 86_400 (24 hours)
|
|
274
322
|
*/
|
|
275
|
-
durableTimeout?: number;
|
|
323
|
+
durableTimeout?: string | number;
|
|
324
|
+
/**
|
|
325
|
+
* Optional debounce configuration, as a duration or a full config
|
|
326
|
+
*/
|
|
327
|
+
debounce?: DebounceInput;
|
|
276
328
|
};
|
|
@@ -53,6 +53,8 @@ export interface BlueprintMediaLibraryConfigResource extends BlueprintInstallati
|
|
|
53
53
|
appType: 'media-library';
|
|
54
54
|
/** The custom fields the Media Library config exposes. */
|
|
55
55
|
fields: BlueprintMediaLibraryField[];
|
|
56
|
+
/** The local version of `sanity` at deployment. Populated during deployment. */
|
|
57
|
+
version?: string;
|
|
56
58
|
}
|
|
57
59
|
/**
|
|
58
60
|
* A union of every singleton installation config resource, discriminated by `appType`.
|
|
@@ -65,20 +65,26 @@ export interface BlueprintResourceLifecycle {
|
|
|
65
65
|
deletionPolicy?: BlueprintResourceDeletionPolicy;
|
|
66
66
|
ownershipAction?: BlueprintOwnershipAction;
|
|
67
67
|
/**
|
|
68
|
-
* Declares a dependency on another resource in the blueprint.
|
|
69
|
-
* The referenced
|
|
68
|
+
* Declares a dependency on another resource or list of resources in the blueprint.
|
|
69
|
+
* The referenced resources will be deployed before this one.
|
|
70
70
|
*
|
|
71
|
-
* The
|
|
72
|
-
* the
|
|
71
|
+
* The values must be resource references starting with `$.resources.` followed by the name of
|
|
72
|
+
* the resources this resource depends on.
|
|
73
73
|
*
|
|
74
74
|
* @example
|
|
75
75
|
* ```ts
|
|
76
|
+
* // single dependency
|
|
76
77
|
* lifecycle: {
|
|
77
78
|
* dependsOn: '$.resources.my-dataset',
|
|
78
79
|
* }
|
|
80
|
+
*
|
|
81
|
+
* // multiple dependencies
|
|
82
|
+
* lifecycle: {
|
|
83
|
+
* dependsOn: ['$.resources.my-project', '$.resources.my-dataset'],
|
|
84
|
+
* }
|
|
79
85
|
* ```
|
|
80
86
|
*/
|
|
81
|
-
dependsOn?: string;
|
|
87
|
+
dependsOn?: string | string[];
|
|
82
88
|
}
|
|
83
89
|
/**
|
|
84
90
|
* Defines the lifcycle policy for this resource.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { BlueprintError } from '../types/errors.js';
|
|
2
|
+
import type { DebounceConfig, DebounceInput } from '../types/functions/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Normalizes the debounce config accepted by a definer into the shape held by a resource,
|
|
5
|
+
* parsing any duration into seconds. A bare duration is shorthand for `{window: <duration>}`.
|
|
6
|
+
* @param debounce The debounce config or duration to normalize
|
|
7
|
+
* @internal
|
|
8
|
+
* @returns The debounce config, with `window` and `maxWindow` in seconds
|
|
9
|
+
*/
|
|
10
|
+
export declare function createDebounceObject(debounce: DebounceInput): DebounceConfig;
|
|
11
|
+
/**
|
|
12
|
+
* Resolves a debounce duration, given either as a number of seconds or as a duration string, to seconds.
|
|
13
|
+
* @param field The name of the field being resolved, used in error messages
|
|
14
|
+
* @param value The value to resolve
|
|
15
|
+
* @returns The value in seconds, or the errors that prevented it from being resolved
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveDebounceDuration(field: string, value: unknown): {
|
|
18
|
+
seconds?: number;
|
|
19
|
+
errors: BlueprintError[];
|
|
20
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { parseDuration } from './parse-duration.js';
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes the debounce config accepted by a definer into the shape held by a resource,
|
|
4
|
+
* parsing any duration into seconds. A bare duration is shorthand for `{window: <duration>}`.
|
|
5
|
+
* @param debounce The debounce config or duration to normalize
|
|
6
|
+
* @internal
|
|
7
|
+
* @returns The debounce config, with `window` and `maxWindow` in seconds
|
|
8
|
+
*/
|
|
9
|
+
export function createDebounceObject(debounce) {
|
|
10
|
+
if (typeof debounce !== 'object') {
|
|
11
|
+
return { window: parseDuration(debounce, 's') };
|
|
12
|
+
}
|
|
13
|
+
const { window, maxWindow, key } = debounce;
|
|
14
|
+
// `window` is required, so there is nothing sensible to parse without it
|
|
15
|
+
if (window === undefined) {
|
|
16
|
+
throw new Error('Invalid debounce config: `window` must be provided');
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
window: parseDuration(window, 's'),
|
|
20
|
+
...(maxWindow !== undefined && { maxWindow: parseDuration(maxWindow, 's') }),
|
|
21
|
+
...(key !== undefined && { key }),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Resolves a debounce duration, given either as a number of seconds or as a duration string, to seconds.
|
|
26
|
+
* @param field The name of the field being resolved, used in error messages
|
|
27
|
+
* @param value The value to resolve
|
|
28
|
+
* @returns The value in seconds, or the errors that prevented it from being resolved
|
|
29
|
+
*/
|
|
30
|
+
export function resolveDebounceDuration(field, value) {
|
|
31
|
+
if (typeof value === 'number') {
|
|
32
|
+
return Number.isFinite(value)
|
|
33
|
+
? { seconds: value, errors: [] }
|
|
34
|
+
: { errors: [{ type: 'invalid_value', message: `\`${field}\` must be a valid duration` }] };
|
|
35
|
+
}
|
|
36
|
+
if (typeof value === 'string') {
|
|
37
|
+
try {
|
|
38
|
+
return { seconds: parseDuration(value, 's'), errors: [] };
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return { errors: [{ type: 'invalid_value', message: `\`${field}\` must be a valid duration` }] };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return { errors: [{ type: 'invalid_type', message: `\`${field}\` must be a number of seconds or a duration string` }] };
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=debounce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.js","sourceRoot":"","sources":["../../src/utils/debounce.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAA;AAEjD;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,QAAuB;IAC1D,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACjC,OAAO,EAAC,MAAM,EAAE,aAAa,CAAC,QAAQ,EAAE,GAAG,CAAC,EAAC,CAAA;IAC/C,CAAC;IAED,MAAM,EAAC,MAAM,EAAE,SAAS,EAAE,GAAG,EAAC,GAAG,QAAQ,CAAA;IAEzC,yEAAyE;IACzE,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;IACvE,CAAC;IAED,OAAO;QACL,MAAM,EAAE,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC;QAClC,GAAG,CAAC,SAAS,KAAK,SAAS,IAAI,EAAC,SAAS,EAAE,aAAa,CAAC,SAAS,EAAE,GAAG,CAAC,EAAC,CAAC;QAC1E,GAAG,CAAC,GAAG,KAAK,SAAS,IAAI,EAAC,GAAG,EAAC,CAAC;KAChC,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAAa,EAAE,KAAc;IACnE,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;YAC3B,CAAC,CAAC,EAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAC;YAC9B,CAAC,CAAC,EAAC,MAAM,EAAE,CAAC,EAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,KAAK,KAAK,6BAA6B,EAAC,CAAC,EAAC,CAAA;IAC3F,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,CAAC;YACH,OAAO,EAAC,OAAO,EAAE,aAAa,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,EAAE,EAAC,CAAA;QACzD,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,EAAC,MAAM,EAAE,CAAC,EAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,KAAK,KAAK,6BAA6B,EAAC,CAAC,EAAC,CAAA;QAC9F,CAAC;IACH,CAAC;IAED,OAAO,EAAC,MAAM,EAAE,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,KAAK,KAAK,qDAAqD,EAAC,CAAC,EAAC,CAAA;AACrH,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type DurationUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'mo' | 'y';
|
|
2
|
+
/**
|
|
3
|
+
* Small parse-duration wrapper for string or number
|
|
4
|
+
* @param duration
|
|
5
|
+
* @param unit
|
|
6
|
+
* @return parsed number in the specified unit
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseDuration(duration: string | number, unit?: DurationUnit): number;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import parse from 'parse-duration';
|
|
2
|
+
const YEAR_IN_SEC = 31_536_000;
|
|
3
|
+
const YEAR_IN_MS = YEAR_IN_SEC * 1000;
|
|
4
|
+
const units = parse.unit;
|
|
5
|
+
// Extend the units object because of the calculations of parse-duration of year to secs - `31557600000`
|
|
6
|
+
Object.assign(units, {
|
|
7
|
+
year: YEAR_IN_MS,
|
|
8
|
+
yr: YEAR_IN_MS,
|
|
9
|
+
y: YEAR_IN_MS,
|
|
10
|
+
month: YEAR_IN_MS / 12,
|
|
11
|
+
mth: YEAR_IN_MS / 12,
|
|
12
|
+
mo: YEAR_IN_MS / 12,
|
|
13
|
+
week: YEAR_IN_MS / 52,
|
|
14
|
+
wk: YEAR_IN_MS / 52,
|
|
15
|
+
w: YEAR_IN_MS / 52,
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Small parse-duration wrapper for string or number
|
|
19
|
+
* @param duration
|
|
20
|
+
* @param unit
|
|
21
|
+
* @return parsed number in the specified unit
|
|
22
|
+
*/
|
|
23
|
+
export function parseDuration(duration, unit = 's') {
|
|
24
|
+
const parsed = typeof duration === 'number' ? duration : parse(duration, unit);
|
|
25
|
+
if (parsed === null || !Number.isFinite(parsed)) {
|
|
26
|
+
throw new Error(`Invalid duration: ${duration}`);
|
|
27
|
+
}
|
|
28
|
+
return parsed;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=parse-duration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-duration.js","sourceRoot":"","sources":["../../src/utils/parse-duration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,gBAAgB,CAAA;AAIlC,MAAM,WAAW,GAAG,UAAU,CAAA;AAC9B,MAAM,UAAU,GAAG,WAAW,GAAG,IAAI,CAAA;AACrC,MAAM,KAAK,GAAG,KAAK,CAAC,IAA8B,CAAA;AAElD,wGAAwG;AACxG,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;IACnB,IAAI,EAAE,UAAU;IAChB,EAAE,EAAE,UAAU;IACd,CAAC,EAAE,UAAU;IAEb,KAAK,EAAE,UAAU,GAAG,EAAE;IACtB,GAAG,EAAE,UAAU,GAAG,EAAE;IACpB,EAAE,EAAE,UAAU,GAAG,EAAE;IAEnB,IAAI,EAAE,UAAU,GAAG,EAAE;IACrB,EAAE,EAAE,UAAU,GAAG,EAAE;IACnB,CAAC,EAAE,UAAU,GAAG,EAAE;CACnB,CAAC,CAAA;AAEF;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,QAAyB,EAAE,IAAI,GAAiB,GAAG;IAC/E,MAAM,MAAM,GAAG,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;IAE9E,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CAAC,qBAAqB,QAAQ,EAAE,CAAC,CAAA;IAClD,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -65,3 +65,12 @@ export declare function validatePubSubFunction(functionResource: unknown): Bluep
|
|
|
65
65
|
* @returns Array of validation errors, empty if valid
|
|
66
66
|
*/
|
|
67
67
|
export declare function validateDurableFunction(functionResource: unknown): BlueprintError[];
|
|
68
|
+
/**
|
|
69
|
+
* Validates a debounce config.
|
|
70
|
+
* @param debounce The config to validate
|
|
71
|
+
* @alpha
|
|
72
|
+
* @hidden
|
|
73
|
+
* @category Functions Types
|
|
74
|
+
* @returns Array of validation errors, empty if valid
|
|
75
|
+
*/
|
|
76
|
+
export declare function validateDebounceConfig(debounce: unknown): BlueprintError[];
|