@sanity/runtime-cli 2.5.3 → 2.6.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 +20 -16
- package/dist/actions/functions/test.d.ts +2 -2
- package/dist/actions/functions/test.js +2 -2
- package/dist/commands/functions/test.d.ts +3 -0
- package/dist/commands/functions/test.js +17 -0
- package/dist/server/app.js +48 -4
- package/dist/server/static/api.d.ts +4 -0
- package/dist/server/static/api.js +26 -0
- package/dist/server/static/components/api-base.d.ts +2 -0
- package/dist/server/static/components/dataset-dropdown.d.ts +1 -0
- package/dist/server/static/components/dataset-dropdown.js +45 -0
- package/dist/server/static/components/payload-panel.js +18 -1
- package/dist/server/static/components/projects-dropdown.d.ts +1 -0
- package/dist/server/static/components/projects-dropdown.js +42 -0
- package/dist/server/static/index.html +2 -0
- package/dist/utils/child-process-wrapper.js +27 -1
- package/dist/utils/invoke-local.d.ts +2 -2
- package/dist/utils/invoke-local.js +10 -1
- package/dist/utils/types.d.ts +6 -0
- package/oclif.manifest.json +26 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ $ npm install -g @sanity/runtime-cli
|
|
|
20
20
|
$ sanity-run COMMAND
|
|
21
21
|
running command...
|
|
22
22
|
$ sanity-run (--version)
|
|
23
|
-
@sanity/runtime-cli/2.
|
|
23
|
+
@sanity/runtime-cli/2.6.0 linux-x64 node-v22.14.0
|
|
24
24
|
$ sanity-run --help [COMMAND]
|
|
25
25
|
USAGE
|
|
26
26
|
$ sanity-run COMMAND
|
|
@@ -63,7 +63,7 @@ EXAMPLES
|
|
|
63
63
|
$ sanity-run blueprints add function
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
-
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
66
|
+
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/blueprints/add.ts)_
|
|
67
67
|
|
|
68
68
|
## `sanity-run blueprints config`
|
|
69
69
|
|
|
@@ -85,7 +85,7 @@ EXAMPLES
|
|
|
85
85
|
$ sanity-run blueprints config --edit
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
-
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
88
|
+
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/blueprints/config.ts)_
|
|
89
89
|
|
|
90
90
|
## `sanity-run blueprints deploy`
|
|
91
91
|
|
|
@@ -102,7 +102,7 @@ EXAMPLES
|
|
|
102
102
|
$ sanity-run blueprints deploy
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
-
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
105
|
+
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/blueprints/deploy.ts)_
|
|
106
106
|
|
|
107
107
|
## `sanity-run blueprints info`
|
|
108
108
|
|
|
@@ -124,7 +124,7 @@ EXAMPLES
|
|
|
124
124
|
$ sanity-run blueprints info --id abc123
|
|
125
125
|
```
|
|
126
126
|
|
|
127
|
-
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
127
|
+
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/blueprints/info.ts)_
|
|
128
128
|
|
|
129
129
|
## `sanity-run blueprints init`
|
|
130
130
|
|
|
@@ -141,7 +141,7 @@ EXAMPLES
|
|
|
141
141
|
$ sanity-run blueprints init
|
|
142
142
|
```
|
|
143
143
|
|
|
144
|
-
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
144
|
+
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/blueprints/init.ts)_
|
|
145
145
|
|
|
146
146
|
## `sanity-run blueprints logs`
|
|
147
147
|
|
|
@@ -163,7 +163,7 @@ EXAMPLES
|
|
|
163
163
|
$ sanity-run blueprints logs --watch
|
|
164
164
|
```
|
|
165
165
|
|
|
166
|
-
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
166
|
+
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/blueprints/logs.ts)_
|
|
167
167
|
|
|
168
168
|
## `sanity-run blueprints plan`
|
|
169
169
|
|
|
@@ -180,7 +180,7 @@ EXAMPLES
|
|
|
180
180
|
$ sanity-run blueprints plan
|
|
181
181
|
```
|
|
182
182
|
|
|
183
|
-
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
183
|
+
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/blueprints/plan.ts)_
|
|
184
184
|
|
|
185
185
|
## `sanity-run blueprints stacks`
|
|
186
186
|
|
|
@@ -197,7 +197,7 @@ EXAMPLES
|
|
|
197
197
|
$ sanity-run blueprints stacks
|
|
198
198
|
```
|
|
199
199
|
|
|
200
|
-
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
200
|
+
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/blueprints/stacks.ts)_
|
|
201
201
|
|
|
202
202
|
## `sanity-run functions dev`
|
|
203
203
|
|
|
@@ -217,7 +217,7 @@ EXAMPLES
|
|
|
217
217
|
$ sanity-run functions dev --port 8974
|
|
218
218
|
```
|
|
219
219
|
|
|
220
|
-
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
220
|
+
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/functions/dev.ts)_
|
|
221
221
|
|
|
222
222
|
## `sanity-run functions env add NAME KEY VALUE`
|
|
223
223
|
|
|
@@ -239,7 +239,7 @@ EXAMPLES
|
|
|
239
239
|
$ sanity-run functions env add MyFunction API_URL https://api.example.com/
|
|
240
240
|
```
|
|
241
241
|
|
|
242
|
-
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
242
|
+
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/functions/env/add.ts)_
|
|
243
243
|
|
|
244
244
|
## `sanity-run functions env remove NAME KEY`
|
|
245
245
|
|
|
@@ -260,7 +260,7 @@ EXAMPLES
|
|
|
260
260
|
$ sanity-run functions env remove MyFunction API_URL
|
|
261
261
|
```
|
|
262
262
|
|
|
263
|
-
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
263
|
+
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/functions/env/remove.ts)_
|
|
264
264
|
|
|
265
265
|
## `sanity-run functions invoke NAME`
|
|
266
266
|
|
|
@@ -286,7 +286,7 @@ EXAMPLES
|
|
|
286
286
|
$ sanity-run functions invoke <name> --file 'payload.json'
|
|
287
287
|
```
|
|
288
288
|
|
|
289
|
-
_See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
289
|
+
_See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/functions/invoke.ts)_
|
|
290
290
|
|
|
291
291
|
## `sanity-run functions logs NAME`
|
|
292
292
|
|
|
@@ -314,7 +314,7 @@ EXAMPLES
|
|
|
314
314
|
$ sanity-run functions logs <name> --limit 100
|
|
315
315
|
```
|
|
316
316
|
|
|
317
|
-
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
317
|
+
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/functions/logs.ts)_
|
|
318
318
|
|
|
319
319
|
## `sanity-run functions test NAME`
|
|
320
320
|
|
|
@@ -322,15 +322,19 @@ Invoke a local Sanity Function
|
|
|
322
322
|
|
|
323
323
|
```
|
|
324
324
|
USAGE
|
|
325
|
-
$ sanity-run functions test NAME [-d <value>] [-f <value>] [-t <value>]
|
|
325
|
+
$ sanity-run functions test NAME [-d <value>] [-f <value>] [-t <value>] [-a <value>] [--dataset <value>]
|
|
326
|
+
[--project <value>]
|
|
326
327
|
|
|
327
328
|
ARGUMENTS
|
|
328
329
|
NAME The name of the Sanity Function
|
|
329
330
|
|
|
330
331
|
FLAGS
|
|
332
|
+
-a, --api=<value> Sanity API Version to use
|
|
331
333
|
-d, --data=<value> Data to send to the function
|
|
332
334
|
-f, --file=<value> Read data from file and send to the function
|
|
333
335
|
-t, --timeout=<value> Execution timeout value in seconds
|
|
336
|
+
--dataset=<value> The Sanity dataset to use
|
|
337
|
+
--project=<value> The Sanity project to use
|
|
334
338
|
|
|
335
339
|
DESCRIPTION
|
|
336
340
|
Invoke a local Sanity Function
|
|
@@ -343,7 +347,7 @@ EXAMPLES
|
|
|
343
347
|
$ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
|
|
344
348
|
```
|
|
345
349
|
|
|
346
|
-
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
350
|
+
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v2.6.0/src/commands/functions/test.ts)_
|
|
347
351
|
|
|
348
352
|
## `sanity-run help [COMMAND]`
|
|
349
353
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { InvocationResponse, InvokePayloadOptions } from '../../utils/types.js';
|
|
2
|
-
export declare function testAction(srcPath: string, options: InvokePayloadOptions): Promise<InvocationResponse>;
|
|
1
|
+
import type { InvocationResponse, InvokeContextOptions, InvokePayloadOptions } from '../../utils/types.js';
|
|
2
|
+
export declare function testAction(srcPath: string, options: InvokePayloadOptions, context: InvokeContextOptions): Promise<InvocationResponse>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import buildPayload from '../../utils/build-payload.js';
|
|
2
2
|
import invoke from '../../utils/invoke-local.js';
|
|
3
|
-
export async function testAction(srcPath, options) {
|
|
3
|
+
export async function testAction(srcPath, options, context) {
|
|
4
4
|
const payload = buildPayload(options);
|
|
5
5
|
const { timeout } = options;
|
|
6
6
|
try {
|
|
7
|
-
const { json, logs } = await invoke(srcPath, payload, timeout);
|
|
7
|
+
const { json, logs } = await invoke(srcPath, payload, context, timeout);
|
|
8
8
|
return { error: undefined, json, logs };
|
|
9
9
|
}
|
|
10
10
|
catch (error) {
|
|
@@ -9,6 +9,9 @@ export default class Test extends Command {
|
|
|
9
9
|
data: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
10
|
file: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
11
|
timeout: import("@oclif/core/interfaces").OptionFlag<number | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
api: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
dataset: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
14
|
+
project: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
15
|
};
|
|
13
16
|
run(): Promise<void>;
|
|
14
17
|
}
|
|
@@ -24,6 +24,19 @@ export default class Test extends Command {
|
|
|
24
24
|
description: 'Execution timeout value in seconds',
|
|
25
25
|
required: false,
|
|
26
26
|
}),
|
|
27
|
+
api: Flags.string({
|
|
28
|
+
char: 'a',
|
|
29
|
+
description: 'Sanity API Version to use',
|
|
30
|
+
required: false,
|
|
31
|
+
}),
|
|
32
|
+
dataset: Flags.string({
|
|
33
|
+
description: 'The Sanity dataset to use',
|
|
34
|
+
required: false,
|
|
35
|
+
}),
|
|
36
|
+
project: Flags.string({
|
|
37
|
+
description: 'The Sanity project to use',
|
|
38
|
+
required: false,
|
|
39
|
+
}),
|
|
27
40
|
};
|
|
28
41
|
async run() {
|
|
29
42
|
const { args, flags } = await this.parse(Test);
|
|
@@ -37,6 +50,10 @@ export default class Test extends Command {
|
|
|
37
50
|
data: flags.data,
|
|
38
51
|
file: flags.file,
|
|
39
52
|
timeout: flags.timeout,
|
|
53
|
+
}, {
|
|
54
|
+
apiVersion: flags.api,
|
|
55
|
+
dataset: flags.dataset,
|
|
56
|
+
projectId: flags.project,
|
|
40
57
|
});
|
|
41
58
|
if (!error) {
|
|
42
59
|
this.log('Logs:');
|
package/dist/server/app.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from 'node:fs';
|
|
2
2
|
import { default as mime } from 'mime-types';
|
|
3
3
|
import { readBlueprintOnDisk } from '../actions/blueprints/blueprint.js';
|
|
4
|
+
import config from '../config.js';
|
|
4
5
|
import invoke from '../utils/invoke-local.js';
|
|
5
6
|
import * as http from 'node:http';
|
|
6
7
|
const host = 'localhost';
|
|
7
8
|
const app = (port) => {
|
|
8
9
|
const requestListener = async (req, res) => {
|
|
9
10
|
res.setHeader('Content-Type', 'application/json');
|
|
10
|
-
switch (
|
|
11
|
-
case '/blueprint': {
|
|
11
|
+
switch (true) {
|
|
12
|
+
case req.url === '/blueprint': {
|
|
12
13
|
try {
|
|
13
14
|
const { parsedBlueprint } = await readBlueprintOnDisk({ getStack: false });
|
|
14
15
|
res.setHeader('Content-Type', 'application/json');
|
|
@@ -21,7 +22,7 @@ const app = (port) => {
|
|
|
21
22
|
}
|
|
22
23
|
break;
|
|
23
24
|
}
|
|
24
|
-
case '/invoke': {
|
|
25
|
+
case req.url === '/invoke': {
|
|
25
26
|
if (req.method === 'POST') {
|
|
26
27
|
let body = '';
|
|
27
28
|
req.on('data', (data) => {
|
|
@@ -29,9 +30,10 @@ const app = (port) => {
|
|
|
29
30
|
});
|
|
30
31
|
req.on('end', async () => {
|
|
31
32
|
const { data, func } = JSON.parse(body);
|
|
33
|
+
const { context, event } = data;
|
|
32
34
|
res.setHeader('Content-Type', 'application/json');
|
|
33
35
|
try {
|
|
34
|
-
const response = await invoke(func,
|
|
36
|
+
const response = await invoke(func, event, context);
|
|
35
37
|
res.writeHead(200);
|
|
36
38
|
res.end(JSON.stringify(response));
|
|
37
39
|
}
|
|
@@ -47,6 +49,48 @@ const app = (port) => {
|
|
|
47
49
|
}
|
|
48
50
|
break;
|
|
49
51
|
}
|
|
52
|
+
case req.url === '/projects': {
|
|
53
|
+
res.setHeader('Content-Type', 'application/json');
|
|
54
|
+
try {
|
|
55
|
+
const response = await fetch(`${config.apiUrl}/v2021-06-07/projects?includeOrganizationProjects=true`, {
|
|
56
|
+
headers: {
|
|
57
|
+
Authorization: `Bearer ${config.token}`,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
const json = await response.json();
|
|
61
|
+
res.writeHead(200);
|
|
62
|
+
res.end(JSON.stringify(json));
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
res.writeHead(200);
|
|
66
|
+
res.end(JSON.stringify([]));
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
case req.url?.startsWith('/datasets'): {
|
|
71
|
+
const url = req.url || '';
|
|
72
|
+
const matches = url.match(/[?&]project=([^&]+)/) || [];
|
|
73
|
+
const projectId = matches ? matches[1] : null;
|
|
74
|
+
res.setHeader('Content-Type', 'application/json');
|
|
75
|
+
try {
|
|
76
|
+
let json = [];
|
|
77
|
+
if (projectId) {
|
|
78
|
+
const response = await fetch(`${config.apiUrl}/v2024-08-25/projects/${projectId}/datasets?includeStats=true`, {
|
|
79
|
+
headers: {
|
|
80
|
+
Authorization: `Bearer ${config.token}`,
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
json = await response.json();
|
|
84
|
+
}
|
|
85
|
+
res.writeHead(200);
|
|
86
|
+
res.end(JSON.stringify(json));
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
res.writeHead(200);
|
|
90
|
+
res.end(JSON.stringify([]));
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
50
94
|
default: {
|
|
51
95
|
const requestPath = req.url?.endsWith('/') ? `${req.url}index.html` : req.url;
|
|
52
96
|
const filePath = new URL(`./static${requestPath}`, import.meta.url).pathname;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
export default function API(): {
|
|
2
2
|
blueprint: typeof blueprint;
|
|
3
3
|
invoke: typeof invoke;
|
|
4
|
+
projects: typeof projects;
|
|
5
|
+
datasets: typeof datasets;
|
|
4
6
|
store: any;
|
|
5
7
|
subscribe: any;
|
|
6
8
|
unsubscribe: any;
|
|
7
9
|
};
|
|
8
10
|
declare function blueprint(): void;
|
|
9
11
|
declare function invoke(payloadText?: string): void;
|
|
12
|
+
declare function projects(): void;
|
|
13
|
+
declare function datasets(selectedProject: any): void;
|
|
10
14
|
export {};
|
|
@@ -8,6 +8,8 @@ export default function API() {
|
|
|
8
8
|
return {
|
|
9
9
|
blueprint,
|
|
10
10
|
invoke,
|
|
11
|
+
projects,
|
|
12
|
+
datasets,
|
|
11
13
|
store,
|
|
12
14
|
subscribe: store.subscribe,
|
|
13
15
|
unsubscribe: store.unsubscribe,
|
|
@@ -51,3 +53,27 @@ function blueprint() {
|
|
|
51
53
|
store.functions = []
|
|
52
54
|
})
|
|
53
55
|
}
|
|
56
|
+
|
|
57
|
+
function projects() {
|
|
58
|
+
fetch('/projects')
|
|
59
|
+
.then((response) => response.json())
|
|
60
|
+
.then((projects) => {
|
|
61
|
+
store.projects = projects
|
|
62
|
+
store.selectedProject = projects[0].id
|
|
63
|
+
})
|
|
64
|
+
.catch(() => {
|
|
65
|
+
store.projects = []
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function datasets(selectedProject) {
|
|
70
|
+
fetch(`/datasets?project=${selectedProject}`)
|
|
71
|
+
.then((response) => response.json())
|
|
72
|
+
.then((datasets) => {
|
|
73
|
+
store.datasets = datasets
|
|
74
|
+
store.selectedDataset = datasets[0].name
|
|
75
|
+
})
|
|
76
|
+
.catch(() => {
|
|
77
|
+
store.datasets = []
|
|
78
|
+
})
|
|
79
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/* globals customElements */
|
|
2
|
+
import {ApiBaseElement} from './api-base.js'
|
|
3
|
+
|
|
4
|
+
const template = `<fieldset class="mar-t-sm">
|
|
5
|
+
<label>Dataset</label>
|
|
6
|
+
<select id="dataset">
|
|
7
|
+
</select>
|
|
8
|
+
</fieldset>
|
|
9
|
+
`
|
|
10
|
+
|
|
11
|
+
class DatasetDropdown extends ApiBaseElement {
|
|
12
|
+
datasetSelected = (event) => {
|
|
13
|
+
this.api.store.selectedDataset = event.srcElement.value
|
|
14
|
+
}
|
|
15
|
+
renderDatasets = () => {
|
|
16
|
+
if (this.api.store?.datasets?.length > 0) {
|
|
17
|
+
this.select.innerHTML = this.api.store.datasets
|
|
18
|
+
.map((dataset) => {
|
|
19
|
+
const selected = this.api.store.selectedDataset === dataset.name ? 'selected' : ''
|
|
20
|
+
return `<option value="${dataset.name}" ${selected}>${dataset.name}</option>`
|
|
21
|
+
})
|
|
22
|
+
.join('')
|
|
23
|
+
} else {
|
|
24
|
+
this.select.innerHTML = '<option>No Datasets found</option>'
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
queryDatasets = () => {
|
|
28
|
+
this.api.datasets(this.api.store.selectedProject)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
connectedCallback() {
|
|
32
|
+
this.innerHTML = template
|
|
33
|
+
this.select = this.querySelector('select')
|
|
34
|
+
this.select.addEventListener('change', this.datasetSelected)
|
|
35
|
+
this.api.subscribe(this.renderDatasets, ['datasets'])
|
|
36
|
+
this.api.subscribe(this.queryDatasets, ['selectedProject'])
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
disconnectedCallback() {
|
|
40
|
+
this.select.removeEventListener('change', this.datasetSelected)
|
|
41
|
+
this.api.unsubscribe(this.renderProjects)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
customElements.define('dataset-dropdown', DatasetDropdown)
|
|
@@ -5,13 +5,27 @@ import {ApiBaseElement} from './api-base.js'
|
|
|
5
5
|
const template = `<m-box>
|
|
6
6
|
<h2 class="mar-t-0">Payload</h2>
|
|
7
7
|
<div id="payload" name="payload"></div>
|
|
8
|
+
<form class="mar-t-sm">
|
|
9
|
+
<fieldset>
|
|
10
|
+
<label>API Version</label>
|
|
11
|
+
<input name="apiVersion" id="apiversion">
|
|
12
|
+
</fieldset>
|
|
13
|
+
<projects-dropdown></projects-dropdown>
|
|
14
|
+
<dataset-dropdown></dataset-dropdown>
|
|
15
|
+
</form>
|
|
8
16
|
<button ord="primary" class="mar-t-sm sanity-button">Invoke</button>
|
|
9
17
|
</m-box>
|
|
10
18
|
`
|
|
11
19
|
class PayloadPanel extends ApiBaseElement {
|
|
12
20
|
invoke = () => {
|
|
13
21
|
const payloadText = this.api.store.payload.state.doc.text.join('') || '{}'
|
|
14
|
-
|
|
22
|
+
const event = JSON.parse(payloadText)
|
|
23
|
+
const context = {
|
|
24
|
+
apiVersion: this.apiVersion.value,
|
|
25
|
+
dataset: this.api.store.selectedDataset,
|
|
26
|
+
projectId: this.api.store.selectedProject,
|
|
27
|
+
}
|
|
28
|
+
this.api.invoke({context, event})
|
|
15
29
|
}
|
|
16
30
|
updateButtonText = ({inprogress}) => {
|
|
17
31
|
if (inprogress) {
|
|
@@ -26,6 +40,9 @@ class PayloadPanel extends ApiBaseElement {
|
|
|
26
40
|
connectedCallback() {
|
|
27
41
|
this.innerHTML = template
|
|
28
42
|
this.payload = this.querySelector('#payload')
|
|
43
|
+
this.apiVersion = this.querySelector('#apiversion')
|
|
44
|
+
this.datasetname = this.querySelector('#datasetname')
|
|
45
|
+
this.productId = this.querySelector('#project')
|
|
29
46
|
this.button = this.querySelector('button')
|
|
30
47
|
this.button.addEventListener('click', this.invoke)
|
|
31
48
|
this.api.subscribe(this.updateButtonText, ['inprogress'])
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/* globals customElements */
|
|
2
|
+
import {ApiBaseElement} from './api-base.js'
|
|
3
|
+
|
|
4
|
+
const template = `<fieldset class="mar-t-sm">
|
|
5
|
+
<label>Project</label>
|
|
6
|
+
<select id="project">
|
|
7
|
+
</select>
|
|
8
|
+
</fieldset>
|
|
9
|
+
`
|
|
10
|
+
|
|
11
|
+
class ProjectsDropdown extends ApiBaseElement {
|
|
12
|
+
projectSelected = (event) => {
|
|
13
|
+
this.api.store.selectedProject = event.srcElement.value
|
|
14
|
+
}
|
|
15
|
+
renderProjects = () => {
|
|
16
|
+
if (this.api.store.projects.length > 0) {
|
|
17
|
+
this.select.innerHTML = this.api.store.projects
|
|
18
|
+
.map((proj) => {
|
|
19
|
+
const selected = this.api.store.selectedProject === proj.id ? 'selected' : ''
|
|
20
|
+
return `<option value="${proj.id}" ${selected}>${proj.displayName}</option>`
|
|
21
|
+
})
|
|
22
|
+
.join('')
|
|
23
|
+
} else {
|
|
24
|
+
this.select.innerHTML = '<option>No Projects found</option>'
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
connectedCallback() {
|
|
29
|
+
this.innerHTML = template
|
|
30
|
+
this.select = this.querySelector('select')
|
|
31
|
+
this.select.addEventListener('change', this.projectSelected)
|
|
32
|
+
this.api.subscribe(this.renderProjects, ['projects'])
|
|
33
|
+
this.api.projects()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
disconnectedCallback() {
|
|
37
|
+
this.select.removeEventListener('change', this.projectSelected)
|
|
38
|
+
this.api.unsubscribe(this.renderProjects)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
customElements.define('projects-dropdown', ProjectsDropdown)
|
|
@@ -47,6 +47,8 @@
|
|
|
47
47
|
</div>
|
|
48
48
|
</footer>
|
|
49
49
|
|
|
50
|
+
<script src="./components/dataset-dropdown.js" type="module"></script>
|
|
51
|
+
<script src="./components/projects-dropdown.js" type="module"></script>
|
|
50
52
|
<script src="./components/function-list.js" type="module"></script>
|
|
51
53
|
<script src="./components/network-spinner.js" type="module"></script>
|
|
52
54
|
<script src="./components/payload-panel.js" type="module"></script>
|
|
@@ -13,6 +13,30 @@ export function getFunctionSource(src) {
|
|
|
13
13
|
return src
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
async function enhanceContext(context = {}) {
|
|
17
|
+
const {apiHost, apiVersion, dataset, projectId, token} = context
|
|
18
|
+
const enhancedContext = {...context}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
if (apiHost && apiVersion && dataset && projectId && token) {
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
const {createClient} = await import('@sanity/client')
|
|
24
|
+
const client = createClient({
|
|
25
|
+
projectId,
|
|
26
|
+
dataset,
|
|
27
|
+
useCdn: true,
|
|
28
|
+
apiHost,
|
|
29
|
+
apiVersion,
|
|
30
|
+
token,
|
|
31
|
+
})
|
|
32
|
+
enhancedContext.client = client
|
|
33
|
+
}
|
|
34
|
+
} catch {
|
|
35
|
+
// ignore as user hasn't provided the @sanity/client
|
|
36
|
+
}
|
|
37
|
+
return enhancedContext
|
|
38
|
+
}
|
|
39
|
+
|
|
16
40
|
// Start when payload data arrives from parent process
|
|
17
41
|
process.on('message', async (data) => {
|
|
18
42
|
let jsonData = null
|
|
@@ -23,6 +47,8 @@ process.on('message', async (data) => {
|
|
|
23
47
|
return
|
|
24
48
|
}
|
|
25
49
|
const {srcPath, payload} = jsonData
|
|
50
|
+
const {context, ...event} = payload
|
|
51
|
+
const enhancedContext = await enhanceContext(context)
|
|
26
52
|
let logs = ''
|
|
27
53
|
let json = null
|
|
28
54
|
|
|
@@ -41,7 +67,7 @@ process.on('message', async (data) => {
|
|
|
41
67
|
}
|
|
42
68
|
|
|
43
69
|
if (handler && typeof handler === 'function') {
|
|
44
|
-
json = await handler(
|
|
70
|
+
json = await handler({context: enhancedContext, event})
|
|
45
71
|
}
|
|
46
72
|
|
|
47
73
|
// revert changes to stdout
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { InvocationResponse } from './types.js';
|
|
2
|
-
export default function invoke(srcPath: string,
|
|
1
|
+
import type { InvocationResponse, InvokeContextOptions } from './types.js';
|
|
2
|
+
export default function invoke(srcPath: string, data: null | object, context: InvokeContextOptions, timeout?: number): Promise<InvocationResponse>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { cwd } from 'node:process';
|
|
3
3
|
import { setTimeout } from 'node:timers';
|
|
4
|
+
import config from '../config.js';
|
|
4
5
|
function getChildProcessWrapperPath() {
|
|
5
6
|
return new URL('./child-process-wrapper.js', import.meta.url).pathname;
|
|
6
7
|
}
|
|
7
|
-
export default async function invoke(srcPath,
|
|
8
|
+
export default async function invoke(srcPath, data, context, timeout = 5) {
|
|
8
9
|
return new Promise((resolve, reject) => {
|
|
9
10
|
let child;
|
|
10
11
|
let timer;
|
|
@@ -36,6 +37,14 @@ export default async function invoke(srcPath, payload, timeout = 5) {
|
|
|
36
37
|
shutdown();
|
|
37
38
|
reject(new Error(`Timed out after hitting its ${timeout}s timeout!`));
|
|
38
39
|
}, timeout * 1000);
|
|
40
|
+
const payload = {
|
|
41
|
+
...data,
|
|
42
|
+
context: {
|
|
43
|
+
apiHost: config.apiUrl,
|
|
44
|
+
token: config.token,
|
|
45
|
+
...context,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
39
48
|
child.send(JSON.stringify({ srcPath, payload }, null, 2));
|
|
40
49
|
}
|
|
41
50
|
function shutdown() {
|
package/dist/utils/types.d.ts
CHANGED
|
@@ -72,6 +72,12 @@ export interface InvokePayloadOptions {
|
|
|
72
72
|
timeout?: number;
|
|
73
73
|
}
|
|
74
74
|
/** @internal */
|
|
75
|
+
export interface InvokeContextOptions {
|
|
76
|
+
apiVersion?: string;
|
|
77
|
+
dataset?: string;
|
|
78
|
+
projectId?: string;
|
|
79
|
+
}
|
|
80
|
+
/** @internal */
|
|
75
81
|
export interface InvocationResponse {
|
|
76
82
|
error: undefined | unknown;
|
|
77
83
|
json: object | undefined;
|
package/oclif.manifest.json
CHANGED
|
@@ -406,6 +406,31 @@
|
|
|
406
406
|
"hasDynamicHelp": false,
|
|
407
407
|
"multiple": false,
|
|
408
408
|
"type": "option"
|
|
409
|
+
},
|
|
410
|
+
"api": {
|
|
411
|
+
"char": "a",
|
|
412
|
+
"description": "Sanity API Version to use",
|
|
413
|
+
"name": "api",
|
|
414
|
+
"required": false,
|
|
415
|
+
"hasDynamicHelp": false,
|
|
416
|
+
"multiple": false,
|
|
417
|
+
"type": "option"
|
|
418
|
+
},
|
|
419
|
+
"dataset": {
|
|
420
|
+
"description": "The Sanity dataset to use",
|
|
421
|
+
"name": "dataset",
|
|
422
|
+
"required": false,
|
|
423
|
+
"hasDynamicHelp": false,
|
|
424
|
+
"multiple": false,
|
|
425
|
+
"type": "option"
|
|
426
|
+
},
|
|
427
|
+
"project": {
|
|
428
|
+
"description": "The Sanity project to use",
|
|
429
|
+
"name": "project",
|
|
430
|
+
"required": false,
|
|
431
|
+
"hasDynamicHelp": false,
|
|
432
|
+
"multiple": false,
|
|
433
|
+
"type": "option"
|
|
409
434
|
}
|
|
410
435
|
},
|
|
411
436
|
"hasDynamicHelp": false,
|
|
@@ -502,5 +527,5 @@
|
|
|
502
527
|
]
|
|
503
528
|
}
|
|
504
529
|
},
|
|
505
|
-
"version": "2.
|
|
530
|
+
"version": "2.6.0"
|
|
506
531
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/runtime-cli",
|
|
3
3
|
"description": "Sanity's Runtime CLI for Blueprints and Functions",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.6.0",
|
|
5
5
|
"author": "Sanity Runtime Team",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"@enhance/store": "^1.0.2",
|
|
60
60
|
"@oclif/test": "^4.1.12",
|
|
61
61
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
62
|
+
"@sanity/client": "^6.29.0",
|
|
62
63
|
"@types/adm-zip": "^0.5.7",
|
|
63
64
|
"@types/mime-types": "^2.1.4",
|
|
64
65
|
"@types/node": "18",
|