@skipruntime/helpers 0.0.1 → 0.0.3

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/src/rest.ts CHANGED
@@ -1,23 +1,23 @@
1
- import type { TJSON, Entry, ReactiveResponse } from "@skipruntime/api";
2
- import { parseReactiveResponse } from "./utils.js";
1
+ import type { Json, Entry } from "@skipruntime/api";
3
2
 
4
3
  export type Entrypoint = {
5
4
  host: string;
6
- port: number;
5
+ streaming_port: number;
6
+ control_port: number;
7
7
  secured?: boolean;
8
8
  };
9
9
 
10
10
  function toHttp(entrypoint: Entrypoint) {
11
11
  if (entrypoint.secured)
12
- return `https://${entrypoint.host}:${entrypoint.port}`;
13
- return `http://${entrypoint.host}:${entrypoint.port}`;
12
+ return `https://${entrypoint.host}:${entrypoint.control_port}`;
13
+ return `http://${entrypoint.host}:${entrypoint.control_port}`;
14
14
  }
15
15
 
16
16
  export async function fetchJSON<V>(
17
17
  url: string,
18
18
  method: "POST" | "GET" | "PUT" | "PATCH" | "HEAD" | "DELETE" = "GET",
19
- headers: Record<string, string> = {},
20
- data?: TJSON,
19
+ headers: { [header: string]: string } = {},
20
+ data?: Json,
21
21
  ): Promise<[V | null, Headers]> {
22
22
  const body = data ? JSON.stringify(data) : undefined;
23
23
  const response = await fetch(url, {
@@ -41,122 +41,66 @@ export async function fetchJSON<V>(
41
41
  return [responseJSON, response.headers];
42
42
  }
43
43
 
44
- export class SkipRESTService {
44
+ export class RESTWrapperOfSkipService {
45
45
  private entrypoint: string;
46
46
 
47
47
  constructor(
48
48
  entrypoint: Entrypoint = {
49
49
  host: "localhost",
50
- port: 3587,
50
+ streaming_port: 8080,
51
+ control_port: 8081,
51
52
  },
52
53
  ) {
53
54
  this.entrypoint = toHttp(entrypoint);
54
55
  }
55
56
 
56
- async getAll<K extends TJSON, V extends TJSON>(
57
+ async getAll<K extends Json, V extends Json>(
57
58
  resource: string,
58
- params: Record<string, string>,
59
- reactiveAuth?: Uint8Array | string,
60
- ): Promise<{ values: Entry<K, V>[]; reactive?: ReactiveResponse }> {
59
+ params: { [param: string]: string },
60
+ ): Promise<Entry<K, V>[]> {
61
61
  const qParams = new URLSearchParams(params).toString();
62
- let header = {};
63
- if (reactiveAuth) {
64
- if (typeof reactiveAuth == "string") {
65
- header = {
66
- "X-Reactive-Auth": reactiveAuth,
67
- };
68
- } else {
69
- header = {
70
- "X-Reactive-Auth": Buffer.from(reactiveAuth.buffer).toString(
71
- "base64",
72
- ),
73
- };
74
- }
75
- }
76
- const [optValues, headers] = await fetchJSON<Entry<K, V>[]>(
77
- `${this.entrypoint}/v1/${resource}?${qParams}`,
62
+ const [optValues, _headers] = await fetchJSON<Entry<K, V>[]>(
63
+ `${this.entrypoint}/v1/resources/${resource}?${qParams}`,
78
64
  "GET",
79
- header,
80
65
  );
81
- const reactive = parseReactiveResponse(headers);
82
66
  const values = optValues ?? [];
83
- return reactive ? { values, reactive } : { values };
67
+ return values;
84
68
  }
85
69
 
86
- async head(
70
+ async getArray<V extends Json>(
87
71
  resource: string,
88
- params: Record<string, string>,
89
- reactiveAuth: Uint8Array | string,
90
- ): Promise<ReactiveResponse> {
91
- const qParams = new URLSearchParams(params).toString();
92
- const header = this.header(reactiveAuth);
93
- const [_data, headers] = await fetchJSON(
94
- `${this.entrypoint}/v1/${resource}?${qParams}`,
95
- "HEAD",
96
- header,
97
- );
98
- const reactiveResponse = parseReactiveResponse(headers);
99
- if (!reactiveResponse)
100
- throw new Error("Reactive response must be suplied.");
101
- else {
102
- return reactiveResponse;
103
- }
104
- }
105
-
106
- async getArray<V extends TJSON>(
107
- resource: string,
108
- params: Record<string, string>,
72
+ params: { [param: string]: string },
109
73
  key: string,
110
- reactiveAuth?: Uint8Array | string,
111
74
  ): Promise<V[]> {
112
75
  const qParams = new URLSearchParams(params).toString();
113
- const header = this.header(reactiveAuth);
114
76
  const [data, _headers] = await fetchJSON<V[]>(
115
- `${this.entrypoint}/v1/${resource}/${key}?${qParams}`,
77
+ `${this.entrypoint}/v1/resources/${resource}/${key}?${qParams}`,
116
78
  "GET",
117
- header,
118
79
  );
119
80
  return data ?? [];
120
81
  }
121
82
 
122
- async put<V extends TJSON>(
83
+ async put<K extends Json, V extends Json>(
123
84
  collection: string,
124
- key: string,
85
+ key: K,
125
86
  value: V[],
126
87
  ): Promise<void> {
127
- await fetchJSON(
128
- `${this.entrypoint}/v1/${collection}/${key}`,
129
- "PUT",
130
- {},
131
- value,
132
- );
88
+ return await this.patch(collection, [[key, value]]);
133
89
  }
134
90
 
135
- async patch<K extends TJSON, V extends TJSON>(
91
+ async patch<K extends Json, V extends Json>(
136
92
  collection: string,
137
93
  values: Entry<K, V>[],
138
94
  ): Promise<void> {
139
- await fetchJSON(`${this.entrypoint}/v1/${collection}`, "PATCH", {}, values);
140
- }
141
-
142
- async deleteKey(collection: string, key: string): Promise<void> {
143
- await fetchJSON(`${this.entrypoint}/v1/${collection}/${key}`, "DELETE", {});
95
+ await fetchJSON(
96
+ `${this.entrypoint}/v1/inputs/${collection}`,
97
+ "PATCH",
98
+ {},
99
+ values,
100
+ );
144
101
  }
145
102
 
146
- private header(reactiveAuth?: Uint8Array | string): Record<string, string> {
147
- if (reactiveAuth) {
148
- if (typeof reactiveAuth == "string") {
149
- return {
150
- "X-Reactive-Auth": reactiveAuth,
151
- };
152
- } else {
153
- return {
154
- "X-Reactive-Auth": Buffer.from(reactiveAuth.buffer).toString(
155
- "base64",
156
- ),
157
- };
158
- }
159
- }
160
- return {};
103
+ async deleteKey<K extends Json>(collection: string, key: K): Promise<void> {
104
+ return await this.patch(collection, [[key, []]]);
161
105
  }
162
106
  }
package/src/utils.ts CHANGED
@@ -1,78 +1,48 @@
1
- import type { Opt } from "@skip-wasm/std";
1
+ import type { Nullable } from "@skip-wasm/std";
2
2
  import { ManyToOneMapper } from "@skipruntime/api";
3
- import type {
4
- Accumulator,
5
- NonEmptyIterator,
6
- ReactiveResponse,
7
- TJSON,
8
- } from "@skipruntime/api";
3
+ import type { Reducer, NonEmptyIterator, Json } from "@skipruntime/api";
9
4
 
10
- export class Sum implements Accumulator<number, number> {
5
+ export class Sum implements Reducer<number, number> {
11
6
  default = 0;
12
7
 
13
- accumulate(acc: number, value: number): number {
8
+ add(acc: number, value: number): number {
14
9
  return acc + value;
15
10
  }
16
11
 
17
- dismiss(acc: number, value: number): Opt<number> {
12
+ remove(acc: number, value: number): Nullable<number> {
18
13
  return acc - value;
19
14
  }
20
15
  }
21
16
 
22
- export class Min implements Accumulator<number, number> {
17
+ export class Min implements Reducer<number, number> {
23
18
  default = null;
24
19
 
25
- accumulate(acc: Opt<number>, value: number): number {
20
+ add(acc: Nullable<number>, value: number): number {
26
21
  return acc === null ? value : Math.min(acc, value);
27
22
  }
28
23
 
29
- dismiss(acc: number, value: number): Opt<number> {
24
+ remove(acc: number, value: number): Nullable<number> {
30
25
  return value > acc ? acc : null;
31
26
  }
32
27
  }
33
28
 
34
- export class Max implements Accumulator<number, number> {
29
+ export class Max implements Reducer<number, number> {
35
30
  default = null;
36
31
 
37
- accumulate(acc: Opt<number>, value: number): number {
32
+ add(acc: Nullable<number>, value: number): number {
38
33
  return acc === null ? value : Math.max(acc, value);
39
34
  }
40
35
 
41
- dismiss(acc: number, value: number): Opt<number> {
36
+ remove(acc: number, value: number): Nullable<number> {
42
37
  return value < acc ? acc : null;
43
38
  }
44
39
  }
45
40
 
46
41
  export class CountMapper<
47
- K extends TJSON,
48
- V extends TJSON,
42
+ K extends Json,
43
+ V extends Json,
49
44
  > extends ManyToOneMapper<K, V, number> {
50
45
  mapValues(values: NonEmptyIterator<V>): number {
51
46
  return values.toArray().length;
52
47
  }
53
48
  }
54
-
55
- export function parseReactiveResponse(
56
- header: Headers | string,
57
- ): ReactiveResponse | undefined {
58
- const strReactiveResponse =
59
- typeof header == "string"
60
- ? header
61
- : header.get("Skip-Reactive-Response-Token");
62
- if (!strReactiveResponse) return undefined;
63
- return JSON.parse(strReactiveResponse, (key: string, value: string) => {
64
- if (key == "watermark") return BigInt(value);
65
- return value;
66
- }) as ReactiveResponse;
67
- }
68
-
69
- export function reactiveResponseHeader(
70
- reactiveResponse: ReactiveResponse,
71
- ): [string, string] {
72
- return [
73
- "Skip-Reactive-Response-Token",
74
- JSON.stringify(reactiveResponse, (_key: string, value: unknown) =>
75
- typeof value === "bigint" ? value.toString() : value,
76
- ),
77
- ];
78
- }