@sanity/functions 1.3.1 → 1.5.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/dist/definers.d.ts +9 -3
- package/dist/definers.js +12 -2
- package/dist/definers.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/invoke.d.ts +18 -0
- package/dist/invoke.js +117 -0
- package/dist/invoke.js.map +1 -0
- package/dist/pipelines.d.ts +32 -0
- package/dist/pipelines.js +74 -0
- package/dist/pipelines.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +135 -24
- package/package.json +19 -3
- package/src/aws-lite-plugins.d.ts +4 -0
- package/src/definers.ts +13 -3
- package/src/index.ts +2 -0
- package/src/invoke.ts +135 -0
- package/src/pipelines.ts +107 -0
- package/src/types.ts +145 -24
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Callback provided by Sanity CLI when doing local development
|
|
3
|
+
*/
|
|
4
|
+
export type InvokeCallback = <T = unknown>(name: string, payload: FunctionPayload, options?: InvokeOptions) => Promise<T>;
|
|
5
|
+
/**
|
|
6
|
+
* Options provided to invoke
|
|
7
|
+
*/
|
|
8
|
+
export type InvokeOptions = {
|
|
9
|
+
sync?: boolean;
|
|
10
|
+
};
|
|
1
11
|
/**
|
|
2
12
|
* The context object passed to the function handler.
|
|
3
|
-
*
|
|
4
|
-
* @beta
|
|
5
13
|
*/
|
|
6
14
|
export interface FunctionContext {
|
|
7
15
|
/** The resource type of the event source; resource type that invoked the function. */
|
|
@@ -12,12 +20,17 @@ export interface FunctionContext {
|
|
|
12
20
|
functionResourceType: string;
|
|
13
21
|
/** The resource ID of the function container; resource ID that houses the function. */
|
|
14
22
|
functionResourceId: string;
|
|
23
|
+
/**
|
|
24
|
+
* uniqueId for a pipeline run
|
|
25
|
+
*/
|
|
26
|
+
runId?: string;
|
|
15
27
|
/**
|
|
16
28
|
* `local` is set to `true` when testing your function locally.
|
|
17
29
|
* i.e. `sanity function test func-name`
|
|
18
30
|
* Otherwise, the property is not set.
|
|
19
31
|
*/
|
|
20
32
|
local?: boolean;
|
|
33
|
+
invoke?: InvokeCallback;
|
|
21
34
|
/**
|
|
22
35
|
* Options that can be passed to a `@sanity/client` constructor to configure it
|
|
23
36
|
* against the project and dataset which triggered the event. Note that you should
|
|
@@ -31,7 +44,6 @@ export interface FunctionContext {
|
|
|
31
44
|
* ...context.clientOptions,
|
|
32
45
|
* })
|
|
33
46
|
* ```
|
|
34
|
-
* @beta
|
|
35
47
|
*/
|
|
36
48
|
clientOptions: {
|
|
37
49
|
apiHost?: string;
|
|
@@ -39,11 +51,11 @@ export interface FunctionContext {
|
|
|
39
51
|
projectId: string;
|
|
40
52
|
token: string;
|
|
41
53
|
};
|
|
54
|
+
/** Resource interface that allows access to Blueprint Resources */
|
|
55
|
+
resources: ResourcesApi;
|
|
42
56
|
}
|
|
43
57
|
/**
|
|
44
58
|
* The context object passed to the schedule function handler.
|
|
45
|
-
*
|
|
46
|
-
* @beta
|
|
47
59
|
*/
|
|
48
60
|
export interface ScheduledFunctionContext {
|
|
49
61
|
/**
|
|
@@ -52,6 +64,7 @@ export interface ScheduledFunctionContext {
|
|
|
52
64
|
* Otherwise, the property is not set.
|
|
53
65
|
*/
|
|
54
66
|
local?: boolean;
|
|
67
|
+
invoke?: InvokeCallback;
|
|
55
68
|
/**
|
|
56
69
|
* Options that can be passed to a `@sanity/client` constructor to configure it
|
|
57
70
|
* against the project and dataset which triggered the event. Note that you should
|
|
@@ -65,7 +78,6 @@ export interface ScheduledFunctionContext {
|
|
|
65
78
|
* ...context.clientOptions,
|
|
66
79
|
* })
|
|
67
80
|
* ```
|
|
68
|
-
* @beta
|
|
69
81
|
*/
|
|
70
82
|
clientOptions?: {
|
|
71
83
|
apiHost?: string;
|
|
@@ -73,33 +85,27 @@ export interface ScheduledFunctionContext {
|
|
|
73
85
|
projectId?: string;
|
|
74
86
|
token?: string;
|
|
75
87
|
};
|
|
88
|
+
/** Resource interface that allows access to Blueprint Resources */
|
|
89
|
+
resources: ResourcesApi;
|
|
76
90
|
}
|
|
77
91
|
/**
|
|
78
92
|
* The event object received by the function handler in the case of a document event,
|
|
79
93
|
* such as a publish, unpublish, delete or mutation event and similar.
|
|
80
|
-
*
|
|
81
|
-
* @beta
|
|
82
94
|
*/
|
|
83
95
|
export interface DocumentEvent<IData = any> {
|
|
84
96
|
/**
|
|
85
97
|
* The data delivered to the function. This is the result of applying any configured
|
|
86
98
|
* GROQ-projection to the changed document. If no projection is configured, this is
|
|
87
99
|
* the document itself.
|
|
88
|
-
*
|
|
89
|
-
* @beta
|
|
90
100
|
*/
|
|
91
101
|
data: IData;
|
|
92
102
|
}
|
|
93
103
|
/**
|
|
94
104
|
* The event object received by the function handler in the case of a sync-tag-invalidate event.
|
|
95
|
-
*
|
|
96
|
-
* @beta
|
|
97
105
|
*/
|
|
98
106
|
export interface SyncTagInvalidateEvent {
|
|
99
107
|
/**
|
|
100
108
|
* The sync tags for use with cache invalidation and notifying the callback endpoint once tags are invalidated.
|
|
101
|
-
*
|
|
102
|
-
* @beta
|
|
103
109
|
*/
|
|
104
110
|
data: {
|
|
105
111
|
/** Array of sync tags to be invalidated. */
|
|
@@ -108,8 +114,6 @@ export interface SyncTagInvalidateEvent {
|
|
|
108
114
|
}
|
|
109
115
|
/**
|
|
110
116
|
* A function handler for a document event.
|
|
111
|
-
*
|
|
112
|
-
* @beta
|
|
113
117
|
*/
|
|
114
118
|
export type DocumentEventHandler<IData = any> = (envelope: {
|
|
115
119
|
context: FunctionContext;
|
|
@@ -117,22 +121,37 @@ export type DocumentEventHandler<IData = any> = (envelope: {
|
|
|
117
121
|
}) => void | Promise<void>;
|
|
118
122
|
/**
|
|
119
123
|
* A function handler for a schedule event.
|
|
120
|
-
*
|
|
121
|
-
* @beta
|
|
122
124
|
*/
|
|
123
125
|
export type ScheduledEventHandler = (envelope: {
|
|
124
126
|
context: ScheduledFunctionContext;
|
|
125
127
|
}) => void | Promise<void>;
|
|
126
128
|
/**
|
|
127
|
-
* A
|
|
129
|
+
* A generic function context type which supports all function types
|
|
130
|
+
*/
|
|
131
|
+
export type GenericContext = FunctionContext | ScheduledFunctionContext | SyncTagInvalidateContext;
|
|
132
|
+
/**
|
|
133
|
+
* A generic function event type which supports all function types
|
|
134
|
+
*/
|
|
135
|
+
export type GenericEvent = DocumentEvent | SyncTagInvalidateEvent;
|
|
136
|
+
/**
|
|
137
|
+
* A generic function handler that can receive the payload of any function type.
|
|
128
138
|
*
|
|
129
|
-
*
|
|
139
|
+
* The envelope is intentionally permissive so a single handler can be registered
|
|
140
|
+
* regardless of the event source without narrowing: `context` is any supported
|
|
141
|
+
* context, `event` is any supported event, and `done` is optional (present only
|
|
142
|
+
* for sync-tag-invalidate events).
|
|
143
|
+
*/
|
|
144
|
+
export type EventHandler<IData = any> = (envelope: {
|
|
145
|
+
context: GenericContext;
|
|
146
|
+
event: DocumentEvent<IData> | SyncTagInvalidateEvent;
|
|
147
|
+
done?: SyncTagInvalidateCallback;
|
|
148
|
+
}) => void | Promise<void>;
|
|
149
|
+
/**
|
|
150
|
+
* A callback function to invoke once a sync-tag-invalidate event has been processed. Signals to Sanity that sync tag invalidation has completed.
|
|
130
151
|
*/
|
|
131
152
|
export type SyncTagInvalidateCallback = (syncTags: string[]) => Promise<Response>;
|
|
132
153
|
/**
|
|
133
154
|
* The context object passed to the sync tag invalidate event handler.
|
|
134
|
-
*
|
|
135
|
-
* @beta
|
|
136
155
|
*/
|
|
137
156
|
export interface SyncTagInvalidateContext extends Omit<FunctionContext, 'clientOptions'> {
|
|
138
157
|
/**
|
|
@@ -149,14 +168,106 @@ export interface SyncTagInvalidateContext extends Omit<FunctionContext, 'clientO
|
|
|
149
168
|
*/
|
|
150
169
|
token?: string;
|
|
151
170
|
};
|
|
171
|
+
/** Resource interface that allows access to Blueprint Resources */
|
|
172
|
+
resources: ResourcesApi;
|
|
152
173
|
}
|
|
153
174
|
/**
|
|
154
175
|
* A function handler for a sync-tag-invalidate event.
|
|
155
|
-
*
|
|
156
|
-
* @beta
|
|
157
176
|
*/
|
|
158
177
|
export type SyncTagInvalidateEventHandler = (envelope: {
|
|
159
178
|
context: SyncTagInvalidateContext;
|
|
160
179
|
event: SyncTagInvalidateEvent;
|
|
161
180
|
done: SyncTagInvalidateCallback;
|
|
162
181
|
}) => void | Promise<void>;
|
|
182
|
+
/**
|
|
183
|
+
* An interface to describe resources found in a Blueprint
|
|
184
|
+
*/
|
|
185
|
+
export interface BlueprintResource {
|
|
186
|
+
id: string;
|
|
187
|
+
name: string;
|
|
188
|
+
type: string;
|
|
189
|
+
}
|
|
190
|
+
export interface ResourcesApi {
|
|
191
|
+
/** Cross-type lookup by name: `context.resources('my-proj')`. */
|
|
192
|
+
(name: string): BlueprintResource | undefined;
|
|
193
|
+
/** Flat array of every resource across types. */
|
|
194
|
+
all(): BlueprintResource[];
|
|
195
|
+
/** Iterate every resource: `for (const r of context.resources)`. */
|
|
196
|
+
[Symbol.iterator](): IterableIterator<BlueprintResource>;
|
|
197
|
+
/** Type specific lookup by name: `context.cors('my-cors')` */
|
|
198
|
+
cors(name: string): BlueprintResource | undefined;
|
|
199
|
+
dataset(name: string): BlueprintResource | undefined;
|
|
200
|
+
function(name: string): BlueprintResource | undefined;
|
|
201
|
+
project(name: string): BlueprintResource | undefined;
|
|
202
|
+
role(name: string): BlueprintResource | undefined;
|
|
203
|
+
webhook(name: string): BlueprintResource | undefined;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* The payload for the `invoke` method
|
|
207
|
+
*/
|
|
208
|
+
export type FunctionPayload = {
|
|
209
|
+
event: GenericEvent;
|
|
210
|
+
context: GenericContext;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* The definition of a function resource in our resource discovery table
|
|
214
|
+
*/
|
|
215
|
+
export type FunctionResource = {
|
|
216
|
+
logicalResourceId: string;
|
|
217
|
+
physicalResourceId: string;
|
|
218
|
+
};
|
|
219
|
+
type FunctionResourceKey = 'eventsourcemapping' | 'function' | 'parameter' | 'queue' | 'schedule' | 'subscription' | 'topic';
|
|
220
|
+
export type FunctionResourceEnvelope = {
|
|
221
|
+
[K in FunctionResourceKey]-?: {
|
|
222
|
+
[P in K]: FunctionResource;
|
|
223
|
+
} & {
|
|
224
|
+
[P in Exclude<FunctionResourceKey, K>]?: FunctionResource;
|
|
225
|
+
};
|
|
226
|
+
}[FunctionResourceKey];
|
|
227
|
+
/**
|
|
228
|
+
* The interface defining the operations available to a pipeline function.
|
|
229
|
+
* These operations are steps that delegate to other functions, and wait for external callbacks.
|
|
230
|
+
* @alpha Using pipeline functions is considered experimental and may change in the future.
|
|
231
|
+
* @public
|
|
232
|
+
* @hidden
|
|
233
|
+
*/
|
|
234
|
+
export interface PipelineOperations {
|
|
235
|
+
/**
|
|
236
|
+
* Runs a named step. Its result is recorded, so if the workflow re-runs the step
|
|
237
|
+
* is not executed again. Failures are retried on their own.
|
|
238
|
+
* similar to ctx.step()
|
|
239
|
+
* @remarks cannot be nested inside another step
|
|
240
|
+
* @param name - Step name
|
|
241
|
+
* @param fn - function being executed
|
|
242
|
+
* @returns The value returned by the executed function
|
|
243
|
+
*/
|
|
244
|
+
run<T>(name: string, fn: () => T | Promise<T>): Promise<T>;
|
|
245
|
+
/**
|
|
246
|
+
* Calls another function and awaits its result.
|
|
247
|
+
* similar to ctx.invoke()
|
|
248
|
+
* @remarks cannot be nested inside another step
|
|
249
|
+
* @param name - Step name
|
|
250
|
+
* @param input - Data passed to the called function
|
|
251
|
+
* @returns The value returned by the called function
|
|
252
|
+
*/
|
|
253
|
+
delegate<T>(name: string, input?: unknown): Promise<T>;
|
|
254
|
+
/**
|
|
255
|
+
* Waits for an external callback
|
|
256
|
+
* similar to ctx.waitForCallback()
|
|
257
|
+
* @remarks cannot be nested inside another step
|
|
258
|
+
* @param name - Step name
|
|
259
|
+
* @param fn -Receives the generated callbackId and returns a delivered promise
|
|
260
|
+
* @returns The value returned by the executed function
|
|
261
|
+
*/
|
|
262
|
+
waitForCallback<T>(name: string, fn: (callbackId: string) => Promise<void>): Promise<T>;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* @alpha Using pipeline functions is considered experimental and may change in the future.
|
|
266
|
+
* @hidden
|
|
267
|
+
*/
|
|
268
|
+
export type PipelineHandler = (envelope: {
|
|
269
|
+
context: FunctionContext;
|
|
270
|
+
event?: GenericEvent;
|
|
271
|
+
step: PipelineOperations;
|
|
272
|
+
}) => unknown | Promise<unknown>;
|
|
273
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/functions",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Helper methods and type definitions for Sanity Functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -47,8 +47,17 @@
|
|
|
47
47
|
"typecheck": "tsc --noEmit --project tsconfig.test.json"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
+
"@aws-lite/client": "^0.23.6",
|
|
51
|
+
"@aws-lite/dynamodb": "^0.3.9",
|
|
52
|
+
"@aws-lite/dynamodb-types": "^0.3.12",
|
|
53
|
+
"@aws-lite/lambda": "^0.1.5",
|
|
54
|
+
"@aws-lite/lambda-types": "^0.1.6",
|
|
55
|
+
"@aws-lite/sns": "^0.0.8",
|
|
56
|
+
"@aws-lite/sns-types": "^0.0.11",
|
|
57
|
+
"@aws-lite/sqs": "^0.2.4",
|
|
58
|
+
"@aws-lite/sqs-types": "^0.2.7",
|
|
50
59
|
"@biomejs/biome": "2.3.12",
|
|
51
|
-
"@types/node": "^
|
|
60
|
+
"@types/node": "^22",
|
|
52
61
|
"@vitest/coverage-v8": "^4.0.18",
|
|
53
62
|
"rimraf": "^6.1.2",
|
|
54
63
|
"typedoc": "^0.28.16",
|
|
@@ -56,9 +65,16 @@
|
|
|
56
65
|
"vitest": "4.0.18"
|
|
57
66
|
},
|
|
58
67
|
"engines": {
|
|
59
|
-
"node": ">=
|
|
68
|
+
"node": ">=22"
|
|
60
69
|
},
|
|
61
70
|
"publishConfig": {
|
|
62
71
|
"access": "public"
|
|
72
|
+
},
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"@aws-lite/client": "^0.23.6",
|
|
75
|
+
"@aws-lite/dynamodb": "^0.3.9",
|
|
76
|
+
"@aws-lite/lambda": "^0.1.5",
|
|
77
|
+
"@aws-lite/sns": "^0.0.8",
|
|
78
|
+
"@aws-lite/sqs": "^0.2.4"
|
|
63
79
|
}
|
|
64
80
|
}
|
package/src/definers.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {DocumentEventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler} from './types.js'
|
|
1
|
+
import type {DocumentEventHandler, EventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler} from './types.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Defines a "document event" function handler.
|
|
@@ -16,8 +16,6 @@ export function documentEventHandler<IData = any>(handler: DocumentEventHandler<
|
|
|
16
16
|
* Defines a "scheduled event" function handler.
|
|
17
17
|
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
18
18
|
*
|
|
19
|
-
* @alpha
|
|
20
|
-
* @hidden
|
|
21
19
|
* @param handler - The event handler function to use.
|
|
22
20
|
* @returns The handler function, unmodified.
|
|
23
21
|
*/
|
|
@@ -37,3 +35,15 @@ export function syncTagInvalidateEventHandler(handler: SyncTagInvalidateEventHan
|
|
|
37
35
|
if (typeof handler !== 'function') throw new TypeError('`handler` must be a function')
|
|
38
36
|
return handler
|
|
39
37
|
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Defines a "generic" function handler.
|
|
41
|
+
* Returns the handler function as-is, only providing the types and doing basic validation.
|
|
42
|
+
*
|
|
43
|
+
* @param handler - The event handler function to use.
|
|
44
|
+
* @returns The handler function, unmodified.
|
|
45
|
+
*/
|
|
46
|
+
export function eventHandler<IData = any>(handler: EventHandler<IData>): EventHandler<IData> {
|
|
47
|
+
if (typeof handler !== 'function') throw new TypeError('`handler` must be a function')
|
|
48
|
+
return handler
|
|
49
|
+
}
|
package/src/index.ts
CHANGED
package/src/invoke.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {Buffer} from 'node:buffer'
|
|
2
|
+
import {env} from 'node:process'
|
|
3
|
+
import type awsLite from '@aws-lite/client'
|
|
4
|
+
import type {FunctionPayload, FunctionResourceEnvelope, InvokeOptions} from './types.js'
|
|
5
|
+
|
|
6
|
+
let awsPromise: Promise<awsLite.AwsLiteClient> | undefined
|
|
7
|
+
|
|
8
|
+
const PARTITION_KEY = 'arc-app-res'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* lazy load aws-lite
|
|
12
|
+
*/
|
|
13
|
+
function getAwsLite() {
|
|
14
|
+
if (!awsPromise) {
|
|
15
|
+
awsPromise = (async () => {
|
|
16
|
+
const {default: awsLite} = await import('@aws-lite/client')
|
|
17
|
+
return awsLite({
|
|
18
|
+
plugins: [import('@aws-lite/dynamodb'), import('@aws-lite/lambda'), import('@aws-lite/sns'), import('@aws-lite/sqs')],
|
|
19
|
+
})
|
|
20
|
+
})().catch((err) => {
|
|
21
|
+
awsPromise = undefined
|
|
22
|
+
throw err
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
return awsPromise
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Gets a specific resource based off of function name
|
|
30
|
+
* @param {string} name
|
|
31
|
+
*/
|
|
32
|
+
async function getResource(name: string, aws: awsLite.AwsLiteClient): Promise<FunctionResourceEnvelope> {
|
|
33
|
+
const TableName = env['SANITY_DISCO']
|
|
34
|
+
if (!TableName) throw new Error('SANITY_DISCO env var not set')
|
|
35
|
+
|
|
36
|
+
const result = await aws.DynamoDB.GetItem({
|
|
37
|
+
TableName,
|
|
38
|
+
Key: {
|
|
39
|
+
PK: PARTITION_KEY,
|
|
40
|
+
SK: name,
|
|
41
|
+
},
|
|
42
|
+
ProjectionExpression: 'resources',
|
|
43
|
+
})
|
|
44
|
+
if (!result?.Item) throw new Error(`Function not found: ${name}`)
|
|
45
|
+
if (!result.Item['resources']) throw new Error(`Resource record for ${name} is missing resources`)
|
|
46
|
+
return result.Item['resources'] as unknown as FunctionResourceEnvelope
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Invokes another Sanity Function.
|
|
51
|
+
*
|
|
52
|
+
* By default the invocation is async: the payload is handed off to the function's
|
|
53
|
+
* event source and nothing is returned. Pass `{sync: true}` to invoke the function and
|
|
54
|
+
* wait for its return value.
|
|
55
|
+
*
|
|
56
|
+
* @param name - Name of the function to invoke
|
|
57
|
+
* @param payload - The `{event, context}` envelope to deliver
|
|
58
|
+
* @param options - Set `sync: true` to wait for and return the function's result
|
|
59
|
+
*/
|
|
60
|
+
export async function invoke<T = unknown>(name: string, payload: FunctionPayload, options: InvokeOptions & {sync: true}): Promise<T>
|
|
61
|
+
export async function invoke(name: string, payload: FunctionPayload, options?: InvokeOptions & {sync?: false}): Promise<void>
|
|
62
|
+
export async function invoke<T = unknown>(name: string, payload: FunctionPayload, options?: InvokeOptions): Promise<T | undefined> {
|
|
63
|
+
if (!name) throw new Error('Function name was not provided')
|
|
64
|
+
const sync = options?.sync ?? false
|
|
65
|
+
|
|
66
|
+
const stringPayload = JSON.stringify(payload)
|
|
67
|
+
// Check to make sure payload is not over the max we can handle
|
|
68
|
+
checkPayloadSize(stringPayload, sync)
|
|
69
|
+
|
|
70
|
+
// Local invoke path for Sanity CLI
|
|
71
|
+
if (payload?.context?.local) {
|
|
72
|
+
if (!payload?.context?.invoke) {
|
|
73
|
+
throw new Error(`No local invoke handler configured for function: ${name}`)
|
|
74
|
+
}
|
|
75
|
+
return await payload.context.invoke(name, payload, options)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const aws = await getAwsLite()
|
|
79
|
+
if (!aws) throw new Error(`Unable to invoke function: ${name}`)
|
|
80
|
+
|
|
81
|
+
// Look up the function details
|
|
82
|
+
const resource = await getResource(name, aws)
|
|
83
|
+
|
|
84
|
+
// Synchronous invocation
|
|
85
|
+
if (sync === true) {
|
|
86
|
+
if (!resource.function) {
|
|
87
|
+
throw new Error(`Function ${name} cannot be invoked synchronously.`)
|
|
88
|
+
}
|
|
89
|
+
const {Payload, FunctionError} = await aws.Lambda.Invoke({
|
|
90
|
+
FunctionName: resource.function.physicalResourceId,
|
|
91
|
+
Payload: payload,
|
|
92
|
+
InvocationType: 'RequestResponse',
|
|
93
|
+
})
|
|
94
|
+
if (FunctionError) {
|
|
95
|
+
const detail = typeof Payload === 'object' && Payload !== null ? (Payload as {errorMessage?: string}).errorMessage : undefined
|
|
96
|
+
throw new Error(`Function ${name} failed: ${detail ?? FunctionError}`)
|
|
97
|
+
}
|
|
98
|
+
// TODO: can we not type cast? might be an aws-lite issue, why is the return type UInt8BlobAdapter
|
|
99
|
+
return Payload as T
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Async invocation by type
|
|
103
|
+
if (resource.topic) {
|
|
104
|
+
await aws.SNS.Publish({
|
|
105
|
+
TopicArn: resource.topic.physicalResourceId,
|
|
106
|
+
Message: stringPayload,
|
|
107
|
+
})
|
|
108
|
+
} else if (resource.queue) {
|
|
109
|
+
await aws.SQS.SendMessage({
|
|
110
|
+
MessageBody: stringPayload,
|
|
111
|
+
QueueUrl: resource.queue.physicalResourceId,
|
|
112
|
+
})
|
|
113
|
+
} else if (resource.function) {
|
|
114
|
+
await aws.Lambda.Invoke({
|
|
115
|
+
FunctionName: resource.function.physicalResourceId,
|
|
116
|
+
Payload: payload,
|
|
117
|
+
InvocationType: 'Event',
|
|
118
|
+
})
|
|
119
|
+
} else {
|
|
120
|
+
throw new Error(`No invokeable resource for function: ${name}`)
|
|
121
|
+
}
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function checkPayloadSize(payload: string, sync: boolean) {
|
|
126
|
+
if (sync) {
|
|
127
|
+
if (Buffer.byteLength(payload, 'utf8') > 6 * 1024 * 1024) {
|
|
128
|
+
throw new Error(`Payload exceeds maximum size of 6MB`)
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
if (Buffer.byteLength(payload, 'utf8') > 256 * 1024) {
|
|
132
|
+
throw new Error(`Payload exceeds maximum size of 256KB`)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
package/src/pipelines.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type {PipelineHandler} from './types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Determine if `createPipeline({}, () => {})` or `createPipeline(() => {})`.
|
|
5
|
+
* @param configOrHandler
|
|
6
|
+
* @param maybeHandler
|
|
7
|
+
* @returns resolved arguments
|
|
8
|
+
*/
|
|
9
|
+
const resolveFuncArgs = <TConfig extends {name: string}>(
|
|
10
|
+
configOrHandler: TConfig | PipelineHandler,
|
|
11
|
+
maybeHandler?: PipelineHandler,
|
|
12
|
+
): {config?: TConfig; handler?: PipelineHandler; hadConfigArg: boolean} => {
|
|
13
|
+
return typeof configOrHandler === 'function'
|
|
14
|
+
? {config: undefined, handler: configOrHandler, hadConfigArg: false}
|
|
15
|
+
: {config: configOrHandler, handler: maybeHandler, hadConfigArg: true}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Validates the config to ensure config contains proper elements
|
|
20
|
+
* @param config
|
|
21
|
+
* @returns Errors while validating a config object
|
|
22
|
+
*/
|
|
23
|
+
const validateConfig = (config: unknown) => {
|
|
24
|
+
const EVENT_TYPES = ['document', 'media-library', 'cron', 'sync-tag-invalidate']
|
|
25
|
+
const errors: string[] = []
|
|
26
|
+
|
|
27
|
+
if (config === undefined || config === null) {
|
|
28
|
+
errors.push('`config` must be defined')
|
|
29
|
+
return errors
|
|
30
|
+
}
|
|
31
|
+
if (typeof config !== 'object') {
|
|
32
|
+
errors.push('`config` must be an object')
|
|
33
|
+
return errors
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const name = 'name' in config ? config.name : undefined
|
|
37
|
+
const event = 'event' in config ? config.event : undefined
|
|
38
|
+
if (typeof name !== 'string') errors.push('`config.name` must be a string')
|
|
39
|
+
|
|
40
|
+
if ('event' in config) {
|
|
41
|
+
if (typeof event !== 'object' || event === null) {
|
|
42
|
+
errors.push('`event` must be an object')
|
|
43
|
+
} else if ('type' in event) {
|
|
44
|
+
if (typeof event.type !== 'string') {
|
|
45
|
+
errors.push('`event.type` must be defined')
|
|
46
|
+
} else if (!EVENT_TYPES.includes(event.type)) {
|
|
47
|
+
errors.push(`\`event.type\` must be one of: ${EVENT_TYPES.join(', ')}`)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return errors
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Pipeline creation function that can be called with or without a config object.
|
|
56
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
57
|
+
* @hidden
|
|
58
|
+
* @param handler
|
|
59
|
+
* @returns The handler function, unmodified.
|
|
60
|
+
*/
|
|
61
|
+
export function createPipeline(handler: PipelineHandler): PipelineHandler & {config?: undefined}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Pipeline creation function that can be called with or without a config object.
|
|
65
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
66
|
+
* @hidden
|
|
67
|
+
* @param config
|
|
68
|
+
* @param handler
|
|
69
|
+
* @returns The handler function, unmodified and the provided config object.
|
|
70
|
+
*/
|
|
71
|
+
export function createPipeline<TConfig extends {name: string}>(
|
|
72
|
+
config: TConfig,
|
|
73
|
+
handler: PipelineHandler,
|
|
74
|
+
): PipelineHandler & {config: TConfig}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Pipeline creation function that can be called with or without a config object.
|
|
78
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
79
|
+
* @hidden
|
|
80
|
+
* @public
|
|
81
|
+
* @param configOrHandler
|
|
82
|
+
* @param maybeHandler
|
|
83
|
+
* @returns The handler function, unmodified and the config object if provided
|
|
84
|
+
*/
|
|
85
|
+
export function createPipeline<TConfig extends {name: string}>(
|
|
86
|
+
configOrHandler: TConfig | PipelineHandler,
|
|
87
|
+
maybeHandler?: PipelineHandler,
|
|
88
|
+
): PipelineHandler {
|
|
89
|
+
const {config, handler, hadConfigArg} = resolveFuncArgs(configOrHandler, maybeHandler)
|
|
90
|
+
const errors = [
|
|
91
|
+
...(hadConfigArg ? validateConfig(config) : []),
|
|
92
|
+
...(typeof handler !== 'function' ? ['`handler` must be a function'] : []),
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
if (errors.length > 0) {
|
|
96
|
+
throw new TypeError(errors.join(', '))
|
|
97
|
+
}
|
|
98
|
+
// Separate config from the handler during build
|
|
99
|
+
return Object.assign(handler as PipelineHandler, {config})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @alpha Pipelines are an experimental feature and may change in the future.
|
|
104
|
+
* @hidden
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
107
|
+
export const pipeline = {createPipeline}
|