@skipruntime/helpers 0.0.5 → 0.0.7
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/external.d.ts +34 -25
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +46 -11
- package/dist/external.js.map +1 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/remote.d.ts +33 -0
- package/dist/remote.d.ts.map +1 -0
- package/dist/remote.js +88 -0
- package/dist/remote.js.map +1 -0
- package/dist/rest.d.ts +80 -40
- package/dist/rest.d.ts.map +1 -1
- package/dist/rest.js +70 -33
- package/dist/rest.js.map +1 -1
- package/package.json +3 -2
- package/src/external.ts +54 -19
- package/src/index.ts +9 -1
- package/src/remote.ts +109 -0
- package/src/rest.ts +100 -55
package/src/rest.ts
CHANGED
|
@@ -7,9 +7,24 @@ import { NonUniqueValueException } from "@skipruntime/api";
|
|
|
7
7
|
* URLs for the service's control and streaming APIs can be constructed from an `Entrypoint`.
|
|
8
8
|
*/
|
|
9
9
|
export type Entrypoint = {
|
|
10
|
+
/**
|
|
11
|
+
* Hostname of the service.
|
|
12
|
+
*/
|
|
10
13
|
host: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Port to use for the service's streaming interface.
|
|
17
|
+
*/
|
|
11
18
|
streaming_port: number;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Port to use for the service's control interface.
|
|
22
|
+
*/
|
|
12
23
|
control_port: number;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Flag that when set indicates that https should be used instead of http.
|
|
27
|
+
*/
|
|
13
28
|
secured?: boolean;
|
|
14
29
|
};
|
|
15
30
|
|
|
@@ -19,20 +34,38 @@ function toHttp(entrypoint: Entrypoint) {
|
|
|
19
34
|
return `http://${entrypoint.host}:${entrypoint.control_port}`;
|
|
20
35
|
}
|
|
21
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Perform an HTTP fetch where input and output data is `Json`.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam V - Type response is *assumed* to have.
|
|
41
|
+
* @param url - URL from which to fetch.
|
|
42
|
+
* @param method - HTTP method of request.
|
|
43
|
+
* @param options - Optional parameters.
|
|
44
|
+
* @param options.headers - Additional headers to add to request.
|
|
45
|
+
* @param options.body - Data to convert to JSON and send in request body.
|
|
46
|
+
* @param options.timeout - Timeout for request, in milliseconds. Defaults to 1000ms.
|
|
47
|
+
* @returns Response parsed as JSON, and headers.
|
|
48
|
+
*/
|
|
22
49
|
export async function fetchJSON<V extends Json>(
|
|
23
50
|
url: string,
|
|
24
51
|
method: "POST" | "GET" | "PUT" | "PATCH" | "HEAD" | "DELETE" = "GET",
|
|
25
|
-
|
|
26
|
-
|
|
52
|
+
options: {
|
|
53
|
+
headers?: { [header: string]: string };
|
|
54
|
+
body?: Json;
|
|
55
|
+
timeout?: number;
|
|
56
|
+
} = {
|
|
57
|
+
headers: {},
|
|
58
|
+
timeout: 1000,
|
|
59
|
+
},
|
|
27
60
|
): Promise<[V | null, Headers]> {
|
|
28
|
-
const body =
|
|
61
|
+
const body = options.body ? JSON.stringify(options.body) : undefined;
|
|
29
62
|
const response = await fetch(url, {
|
|
30
63
|
method,
|
|
31
64
|
body,
|
|
32
65
|
headers: {
|
|
33
66
|
"Content-Type": "application/json",
|
|
34
67
|
Accept: "application/json",
|
|
35
|
-
...headers,
|
|
68
|
+
...options.headers,
|
|
36
69
|
},
|
|
37
70
|
signal: AbortSignal.timeout(1000),
|
|
38
71
|
});
|
|
@@ -58,8 +91,9 @@ export class SkipServiceBroker {
|
|
|
58
91
|
|
|
59
92
|
/**
|
|
60
93
|
* Construct a broker for a Skip service at the given entry point.
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
94
|
+
*
|
|
95
|
+
* @param entrypoint - Entry point of backing service.
|
|
96
|
+
* @returns Method-call broker to service.
|
|
63
97
|
*/
|
|
64
98
|
constructor(
|
|
65
99
|
entrypoint: Entrypoint = {
|
|
@@ -74,58 +108,64 @@ export class SkipServiceBroker {
|
|
|
74
108
|
/**
|
|
75
109
|
* Read the entire contents of a resource.
|
|
76
110
|
*
|
|
77
|
-
* @
|
|
78
|
-
* @
|
|
79
|
-
* @
|
|
111
|
+
* @typeParam K - Type of keys.
|
|
112
|
+
* @typeParam V - Type of values.
|
|
113
|
+
* @param resource - Name of resource, must be a key of the `resources` field of the `SkipService` running at `entrypoint`.
|
|
114
|
+
* @param params - Resource instance parameters.
|
|
115
|
+
* @returns All entries in resource.
|
|
80
116
|
*/
|
|
81
117
|
async getAll<K extends Json, V extends Json>(
|
|
82
118
|
resource: string,
|
|
83
|
-
params:
|
|
119
|
+
params: Json,
|
|
84
120
|
): Promise<Entry<K, V>[]> {
|
|
85
121
|
const [data, _headers] = await fetchJSON<Entry<K, V>[]>(
|
|
86
|
-
`${this.entrypoint}/v1/snapshot`,
|
|
122
|
+
`${this.entrypoint}/v1/snapshot/${resource}`,
|
|
87
123
|
"POST",
|
|
88
|
-
{},
|
|
89
|
-
{ resource, params },
|
|
124
|
+
{ body: params },
|
|
90
125
|
);
|
|
91
126
|
return data ?? [];
|
|
92
127
|
}
|
|
93
128
|
|
|
94
129
|
/**
|
|
95
130
|
* Read the values a resource associates with a single key.
|
|
96
|
-
*
|
|
97
|
-
* @
|
|
98
|
-
* @
|
|
99
|
-
* @
|
|
131
|
+
*
|
|
132
|
+
* @typeParam K - Type of keys.
|
|
133
|
+
* @typeParam V - Type of values.
|
|
134
|
+
* @param resource - Name of resource, must be a key of the `resources` field of the `SkipService` running at `entrypoint`.
|
|
135
|
+
* @param params - Resource instance parameters.
|
|
136
|
+
* @param key - Key to read.
|
|
137
|
+
* @returns The values associated to the key.
|
|
100
138
|
*/
|
|
101
|
-
async getArray<V extends Json>(
|
|
139
|
+
async getArray<K extends Json, V extends Json>(
|
|
102
140
|
resource: string,
|
|
103
|
-
params:
|
|
104
|
-
key:
|
|
141
|
+
params: Json,
|
|
142
|
+
key: K,
|
|
105
143
|
): Promise<V[]> {
|
|
106
144
|
const [data, _headers] = await fetchJSON<V[]>(
|
|
107
|
-
`${this.entrypoint}/v1/snapshot`,
|
|
145
|
+
`${this.entrypoint}/v1/snapshot/${resource}/lookup`,
|
|
108
146
|
"POST",
|
|
109
|
-
{},
|
|
110
|
-
{ resource, key, params },
|
|
147
|
+
{ body: { key, params } },
|
|
111
148
|
);
|
|
112
149
|
return data ?? [];
|
|
113
150
|
}
|
|
114
151
|
|
|
115
152
|
/**
|
|
116
153
|
* Read the single value a resource associates with a key.
|
|
117
|
-
*
|
|
118
|
-
* @
|
|
119
|
-
* @
|
|
120
|
-
* @
|
|
121
|
-
* @
|
|
154
|
+
*
|
|
155
|
+
* @typeParam K - Type of keys.
|
|
156
|
+
* @typeParam V - Type of values.
|
|
157
|
+
* @param resource - Name of resource, must be a key of the `resources` field of the `SkipService` running at `entrypoint`.
|
|
158
|
+
* @param params - Resource instance parameters.
|
|
159
|
+
* @param key - Key to read.
|
|
160
|
+
* @returns The value associated to the key.
|
|
161
|
+
* @throws `NonUniqueValueException` when the key is associated to either zero or multiple values.
|
|
122
162
|
*/
|
|
123
|
-
async getUnique<V extends Json>(
|
|
163
|
+
async getUnique<K extends Json, V extends Json>(
|
|
124
164
|
resource: string,
|
|
125
|
-
params:
|
|
126
|
-
key:
|
|
165
|
+
params: Json,
|
|
166
|
+
key: K,
|
|
127
167
|
): Promise<V> {
|
|
128
|
-
return this.getArray<V>(resource, params, key).then((values) => {
|
|
168
|
+
return this.getArray<K, V>(resource, params, key).then((values) => {
|
|
129
169
|
if (values.length !== 1 || values[0] === undefined)
|
|
130
170
|
throw new NonUniqueValueException();
|
|
131
171
|
return values[0];
|
|
@@ -134,9 +174,12 @@ export class SkipServiceBroker {
|
|
|
134
174
|
|
|
135
175
|
/**
|
|
136
176
|
* Write the values for a single key in a collection.
|
|
137
|
-
*
|
|
138
|
-
* @
|
|
139
|
-
* @
|
|
177
|
+
*
|
|
178
|
+
* @typeParam K - Type of keys.
|
|
179
|
+
* @typeParam V - Type of values.
|
|
180
|
+
* @param collection - Name of the input collection to update, must be a key of the `Inputs` type parameter of the `SkipService` running at `entrypoint`.
|
|
181
|
+
* @param key - Key of entry to write.
|
|
182
|
+
* @param values - Values of entry to write.
|
|
140
183
|
* @returns {void}
|
|
141
184
|
*/
|
|
142
185
|
async put<K extends Json, V extends Json>(
|
|
@@ -149,26 +192,28 @@ export class SkipServiceBroker {
|
|
|
149
192
|
|
|
150
193
|
/**
|
|
151
194
|
* Write multiple entries to a collection.
|
|
152
|
-
*
|
|
153
|
-
* @
|
|
195
|
+
*
|
|
196
|
+
* @typeParam K - Type of keys.
|
|
197
|
+
* @typeParam V - Type of values.
|
|
198
|
+
* @param collection - Name of the input collection to update, must be a key of the `Inputs` type parameter of the `SkipService` running at `entrypoint`.
|
|
199
|
+
* @param entries - Entries to write.
|
|
154
200
|
* @returns {void}
|
|
155
201
|
*/
|
|
156
202
|
async patch<K extends Json, V extends Json>(
|
|
157
203
|
collection: string,
|
|
158
204
|
entries: Entry<K, V>[],
|
|
159
205
|
): Promise<void> {
|
|
160
|
-
await fetchJSON(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
{},
|
|
164
|
-
entries,
|
|
165
|
-
);
|
|
206
|
+
await fetchJSON(`${this.entrypoint}/v1/inputs/${collection}`, "PATCH", {
|
|
207
|
+
body: entries,
|
|
208
|
+
});
|
|
166
209
|
}
|
|
167
210
|
|
|
168
211
|
/**
|
|
169
212
|
* Remove all values associated with a key in a collection.
|
|
170
|
-
*
|
|
171
|
-
* @
|
|
213
|
+
*
|
|
214
|
+
* @typeParam K - Type of keys.
|
|
215
|
+
* @param collection - Name of the input collection to update, must be a key of the `Inputs` type parameter of the `SkipService` running at `entrypoint`.
|
|
216
|
+
* @param key - Key of entry to delete.
|
|
172
217
|
* @returns {void}
|
|
173
218
|
*/
|
|
174
219
|
async deleteKey<K extends Json>(collection: string, key: K): Promise<void> {
|
|
@@ -177,18 +222,18 @@ export class SkipServiceBroker {
|
|
|
177
222
|
|
|
178
223
|
/**
|
|
179
224
|
* Create a resource instance UUID.
|
|
180
|
-
*
|
|
181
|
-
* @
|
|
182
|
-
* @
|
|
225
|
+
*
|
|
226
|
+
* @typeParam K - Type of keys.
|
|
227
|
+
* @typeParam V - Type of values.
|
|
228
|
+
* @param resource - Name of resource, must be a key of the `resources` field of the `SkipService` running at `entrypoint`.
|
|
229
|
+
* @param params - Resource instance parameters.
|
|
230
|
+
* @returns UUID that can be used to subscribe to updates to resource instance.
|
|
183
231
|
*/
|
|
184
|
-
async getStreamUUID(
|
|
185
|
-
resource
|
|
186
|
-
params: { [param: string]: string } = {},
|
|
187
|
-
): Promise<string> {
|
|
188
|
-
return fetch(`${this.entrypoint}/v1/streams`, {
|
|
232
|
+
async getStreamUUID(resource: string, params: Json = {}): Promise<string> {
|
|
233
|
+
return fetch(`${this.entrypoint}/v1/streams/${resource}`, {
|
|
189
234
|
method: "POST",
|
|
190
235
|
headers: { "Content-Type": "application/json" },
|
|
191
|
-
body: JSON.stringify(
|
|
236
|
+
body: JSON.stringify(params),
|
|
192
237
|
}).then((res) => res.text());
|
|
193
238
|
}
|
|
194
239
|
|
|
@@ -197,7 +242,7 @@ export class SkipServiceBroker {
|
|
|
197
242
|
*
|
|
198
243
|
* Under normal circumstances, resource instances are deleted automatically after some period of inactivity; this method enables immediately deleting live streams under exceptional circumstances.
|
|
199
244
|
*
|
|
200
|
-
* @param uuid -
|
|
245
|
+
* @param uuid - Resource instance UUID.
|
|
201
246
|
* @returns {void}
|
|
202
247
|
*/
|
|
203
248
|
async deleteUUID(uuid: string): Promise<void> {
|