spiceflow 1.1.12 → 1.1.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spiceflow",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "description": "Simple API framework with RPC and type safety",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -49,10 +49,11 @@
49
49
  "zod-to-json-schema": "^3.23.2"
50
50
  },
51
51
  "devDependencies": {
52
- "@types/node": "20.2.5"
52
+ "@types/node": "22.7.4"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "cp ../README.md ./README.md && rm -rf dist && tsc",
56
+ "test": "pnpm vitest",
56
57
  "watch": "tsc -w"
57
58
  }
58
59
  }
@@ -1,4 +1,4 @@
1
- export class EdenFetchError<
1
+ export class SpiceflowFetchError<
2
2
  Status = number,
3
3
  Value extends any = any,
4
4
  > extends Error {
@@ -8,8 +8,8 @@ import type { SpiceflowClient } from './types.js'
8
8
 
9
9
  export { SpiceflowClient }
10
10
 
11
- import { EdenFetchError } from './errors.js'
12
- // import { EdenWS } from './ws'
11
+ import { SpiceflowFetchError } from './errors.js'
12
+
13
13
  import { parseStringifiedValue } from './utils.js'
14
14
 
15
15
  const method = [
@@ -161,7 +161,7 @@ export async function* streamSSEResponse(
161
161
  const { done, value: event } = await reader.read()
162
162
  if (done) break
163
163
  if (event?.event === 'error') {
164
- throw new EdenFetchError(500, event.data)
164
+ throw new SpiceflowFetchError(500, event.data)
165
165
  }
166
166
  if (event) {
167
167
  yield tryParsingJson(event.data)
@@ -237,26 +237,6 @@ const createProxy = (
237
237
  }
238
238
  }
239
239
 
240
- // if (method === 'subscribe') {
241
- // const url =
242
- // domain.replace(
243
- // /^([^]+):\/\//,
244
- // domain.startsWith('https://')
245
- // ? 'wss://'
246
- // : domain.startsWith('http://')
247
- // ? 'ws://'
248
- // : locals.find((v) =>
249
- // (domain as string).includes(v)
250
- // )
251
- // ? 'ws://'
252
- // : 'wss://'
253
- // ) +
254
- // path +
255
- // q
256
-
257
- // return new EdenWS(url)
258
- // }
259
-
260
240
  return (async () => {
261
241
  let fetchInit = {
262
242
  method: method?.toUpperCase(),
@@ -404,8 +384,9 @@ const createProxy = (
404
384
  break
405
385
  }
406
386
  } catch (err) {
407
- if (err instanceof EdenFetchError) error = err
408
- else error = new EdenFetchError(422, err)
387
+ if (err instanceof SpiceflowFetchError) error = err
388
+ else
389
+ error = new SpiceflowFetchError(err?.['status'] || 422, err)
409
390
 
410
391
  break
411
392
  }
@@ -449,7 +430,7 @@ const createProxy = (
449
430
  }
450
431
 
451
432
  if (response.status >= 300 || response.status < 200) {
452
- error = new EdenFetchError(response.status, data)
433
+ error = new SpiceflowFetchError(response.status, data)
453
434
  data = null
454
435
  }
455
436
 
@@ -495,4 +476,4 @@ export const createSpiceflowClient = <
495
476
  return createProxy('http://e.ly', config, [], domain)
496
477
  }
497
478
 
498
- export type { SpiceflowClient as Treaty }
479
+
@@ -1,9 +1,7 @@
1
1
  /// <reference lib="dom" />
2
2
  import type { Spiceflow } from '../spiceflow.js'
3
- // import { EdenWS } from './ws'
4
- // import type { IsNever, Not, Prettify } from '../types'
5
- import { EdenFetchError } from './errors.js'
6
- import { EdenWS } from './ws.js'
3
+
4
+ import { SpiceflowFetchError } from './errors.js'
7
5
 
8
6
  export type Prettify<T> = {
9
7
  [K in keyof T]: T[K]
@@ -36,22 +34,22 @@ type ReplaceGeneratorWithAsyncGenerator<
36
34
  ? And<Not<IsNever<A>>, void extends B ? true : false> extends true
37
35
  ? AsyncGenerator<A, B, C>
38
36
  : And<IsNever<A>, void extends B ? false : true> extends true
39
- ? B
40
- : AsyncGenerator<A, B, C> | B
37
+ ? B
38
+ : AsyncGenerator<A, B, C> | B
41
39
  : RecordType[K] extends AsyncGenerator<infer A, infer B, infer C>
42
- ? And<Not<IsNever<A>>, void extends B ? true : false> extends true
43
- ? AsyncGenerator<A, B, C>
44
- : And<IsNever<A>, void extends B ? false : true> extends true
45
- ? B
46
- : AsyncGenerator<A, B, C> | B
47
- : RecordType[K]
40
+ ? And<Not<IsNever<A>>, void extends B ? true : false> extends true
41
+ ? AsyncGenerator<A, B, C>
42
+ : And<IsNever<A>, void extends B ? false : true> extends true
43
+ ? B
44
+ : AsyncGenerator<A, B, C> | B
45
+ : RecordType[K]
48
46
  } & {}
49
47
 
50
48
  type MaybeArray<T> = T | T[]
51
49
  type MaybePromise<T> = T | Promise<T>
52
50
 
53
51
  export namespace SpiceflowClient {
54
- interface TreatyParam {
52
+ interface ClientParam {
55
53
  fetch?: RequestInit
56
54
  }
57
55
 
@@ -60,91 +58,76 @@ export namespace SpiceflowClient {
60
58
  _routes: infer Schema extends Record<string, any>
61
59
  }
62
60
  ? Prettify<Sign<Schema>>
63
- : 'Please install Spiceflow before using Eden'
61
+ : 'Please install Spiceflow before using the client'
64
62
 
65
63
  export type Sign<in out Route extends Record<string, any>> = {
66
- [K in keyof Route as K extends `:${string}`
67
- ? never
68
- : K]: K extends 'subscribe' // ? Websocket route
69
- ? (undefined extends Route['subscribe']['headers']
70
- ? { headers?: Record<string, unknown> }
71
- : {
72
- headers: Route['subscribe']['headers']
73
- }) &
74
- (undefined extends Route['subscribe']['query']
75
- ? { query?: Record<string, unknown> }
76
- : {
77
- query: Route['subscribe']['query']
78
- }) extends infer Param
64
+ [K in keyof Route as K extends `:${string}` ? never : K]: Route[K] extends {
65
+ body: infer Body
66
+ // headers: infer Headers
67
+ params: any
68
+ query: infer Query
69
+ response: infer Response extends Record<number, unknown>
70
+ }
71
+ ? { headers?: Record<string, unknown> } & (undefined extends Query
72
+ ? { query?: Record<string, unknown> }
73
+ : { query: Query }) extends infer Param
79
74
  ? {} extends Param
80
- ? (options?: Param) => EdenWS<Route['subscribe']>
81
- : (options?: Param) => EdenWS<Route['subscribe']>
82
- : never
83
- : Route[K] extends {
84
- body: infer Body
85
- // headers: infer Headers
86
- params: any
87
- query: infer Query
88
- response: infer Response extends Record<number, unknown>
89
- }
90
- ? { headers?: Record<string, unknown> } & (undefined extends Query
91
- ? { query?: Record<string, unknown> }
92
- : { query: Query }) extends infer Param
93
- ? {} extends Param
94
- ? undefined extends Body
95
- ? K extends 'get' | 'head'
96
- ? (
97
- options?: Prettify<Param & TreatyParam>,
98
- ) => Promise<
99
- TreatyResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
100
- >
101
- : (
102
- body?: Body,
103
- options?: Prettify<Param & TreatyParam>,
104
- ) => Promise<
105
- TreatyResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
106
- >
107
- : (
108
- body: Body extends Record<string, unknown>
109
- ? ReplaceBlobWithFiles<Body>
110
- : Body,
111
- options?: Prettify<Param & TreatyParam>,
112
- ) => Promise<
113
- TreatyResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
114
- >
115
- : K extends 'get' | 'head'
75
+ ? undefined extends Body
76
+ ? K extends 'get' | 'head'
116
77
  ? (
117
- options: Prettify<Param & TreatyParam>,
78
+ options?: Prettify<Param & ClientParam>,
118
79
  ) => Promise<
119
- TreatyResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
80
+ ClientResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
120
81
  >
121
82
  : (
122
- body: Body extends Record<string, unknown>
123
- ? ReplaceBlobWithFiles<Body>
124
- : Body,
125
- options: Prettify<Param & TreatyParam>,
83
+ body?: Body,
84
+ options?: Prettify<Param & ClientParam>,
126
85
  ) => Promise<
127
- TreatyResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
86
+ ClientResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
128
87
  >
129
- : never
130
- : CreateParams<Route[K]>
88
+ : (
89
+ body: Body extends Record<string, unknown>
90
+ ? ReplaceBlobWithFiles<Body>
91
+ : Body,
92
+ options?: Prettify<Param & ClientParam>,
93
+ ) => Promise<
94
+ ClientResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
95
+ >
96
+ : K extends 'get' | 'head'
97
+ ? (
98
+ options: Prettify<Param & ClientParam>,
99
+ ) => Promise<
100
+ ClientResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
101
+ >
102
+ : (
103
+ body: Body extends Record<string, unknown>
104
+ ? ReplaceBlobWithFiles<Body>
105
+ : Body,
106
+ options: Prettify<Param & ClientParam>,
107
+ ) => Promise<
108
+ ClientResponse<ReplaceGeneratorWithAsyncGenerator<Response>>
109
+ >
110
+ : never
111
+ : CreateParams<Route[K]>
131
112
  }
132
113
 
133
- type CreateParams<Route extends Record<string, any>> =
134
- Extract<keyof Route, `:${string}`> extends infer Path extends string
135
- ? IsNever<Path> extends true
136
- ? Prettify<Sign<Route>>
137
- : // ! DO NOT USE PRETTIFY ON THIS LINE, OTHERWISE FUNCTION CALLING WILL BE OMITTED
138
- (((params: {
139
- [param in Path extends `:${infer Param}`
140
- ? Param extends `${infer Param}?`
141
- ? Param
142
- : Param
143
- : never]: string | number
144
- }) => Prettify<Sign<Route[Path]>> & CreateParams<Route[Path]>) &
145
- Prettify<Sign<Route>>) &
146
- (Path extends `:${string}?` ? CreateParams<Route[Path]> : {})
147
- : never
114
+ type CreateParams<Route extends Record<string, any>> = Extract<
115
+ keyof Route,
116
+ `:${string}`
117
+ > extends infer Path extends string
118
+ ? IsNever<Path> extends true
119
+ ? Prettify<Sign<Route>>
120
+ : // ! DO NOT USE PRETTIFY ON THIS LINE, OTHERWISE FUNCTION CALLING WILL BE OMITTED
121
+ (((params: {
122
+ [param in Path extends `:${infer Param}`
123
+ ? Param extends `${infer Param}?`
124
+ ? Param
125
+ : Param
126
+ : never]: string | number
127
+ }) => Prettify<Sign<Route[Path]>> & CreateParams<Route[Path]>) &
128
+ Prettify<Sign<Route>>) &
129
+ (Path extends `:${string}?` ? CreateParams<Route[Path]> : {})
130
+ : never
148
131
 
149
132
  export interface Config {
150
133
  // fetch?: Omit<RequestInit, 'headers' | 'method'>
@@ -164,7 +147,7 @@ export namespace SpiceflowClient {
164
147
  // [K in keyof T]: Awaited<T[K]>
165
148
  // }
166
149
 
167
- export type TreatyResponse<Res extends Record<number, unknown>> =
150
+ export type ClientResponse<Res extends Record<number, unknown>> =
168
151
  | {
169
152
  data: Res[200]
170
153
  error: null
@@ -175,9 +158,9 @@ export namespace SpiceflowClient {
175
158
  | {
176
159
  data: null
177
160
  error: Exclude<keyof Res, 200> extends never
178
- ? EdenFetchError<number, any>
161
+ ? SpiceflowFetchError<number, any>
179
162
  : {
180
- [Status in keyof Res]: EdenFetchError<Status, Res[Status]>
163
+ [Status in keyof Res]: SpiceflowFetchError<Status, Res[Status]>
181
164
  }[Exclude<keyof Res, 200>]
182
165
  response: Response
183
166
  status: number
package/src/spiceflow.ts CHANGED
@@ -7,7 +7,7 @@ export { Type as t }
7
7
  import addFormats from 'ajv-formats'
8
8
  import {
9
9
  ComposeSpiceflowResponse,
10
- CreateEden,
10
+ CreateClient,
11
11
  DefinitionBase,
12
12
  ErrorHandler,
13
13
  HTTPMethod,
@@ -311,7 +311,7 @@ export class Spiceflow<
311
311
  Definitions,
312
312
  Metadata,
313
313
  Routes &
314
- CreateEden<
314
+ CreateClient<
315
315
  JoinPath<BasePath, Path>,
316
316
  {
317
317
  post: {
@@ -358,7 +358,7 @@ export class Spiceflow<
358
358
  Definitions,
359
359
  Metadata,
360
360
  Routes &
361
- CreateEden<
361
+ CreateClient<
362
362
  JoinPath<BasePath, Path>,
363
363
  {
364
364
  get: {
@@ -404,7 +404,7 @@ export class Spiceflow<
404
404
  Definitions,
405
405
  Metadata,
406
406
  Routes &
407
- CreateEden<
407
+ CreateClient<
408
408
  JoinPath<BasePath, Path>,
409
409
  {
410
410
  put: {
@@ -451,7 +451,7 @@ export class Spiceflow<
451
451
  Definitions,
452
452
  Metadata,
453
453
  Routes &
454
- CreateEden<
454
+ CreateClient<
455
455
  JoinPath<BasePath, Path>,
456
456
  {
457
457
  patch: {
@@ -498,7 +498,7 @@ export class Spiceflow<
498
498
  Definitions,
499
499
  Metadata,
500
500
  Routes &
501
- CreateEden<
501
+ CreateClient<
502
502
  JoinPath<BasePath, Path>,
503
503
  {
504
504
  delete: {
@@ -545,7 +545,7 @@ export class Spiceflow<
545
545
  Definitions,
546
546
  Metadata,
547
547
  Routes &
548
- CreateEden<
548
+ CreateClient<
549
549
  JoinPath<BasePath, Path>,
550
550
  {
551
551
  options: {
@@ -592,7 +592,7 @@ export class Spiceflow<
592
592
  Definitions,
593
593
  Metadata,
594
594
  Routes &
595
- CreateEden<
595
+ CreateClient<
596
596
  JoinPath<BasePath, Path>,
597
597
  {
598
598
  [method in string]: {
@@ -641,7 +641,7 @@ export class Spiceflow<
641
641
  Definitions,
642
642
  Metadata,
643
643
  Routes &
644
- CreateEden<
644
+ CreateClient<
645
645
  JoinPath<BasePath, Path>,
646
646
  {
647
647
  head: {
@@ -675,7 +675,7 @@ export class Spiceflow<
675
675
  Metadata,
676
676
  BasePath extends ``
677
677
  ? Routes & NewSpiceflow['_routes']
678
- : Routes & CreateEden<BasePath, NewSpiceflow['_routes']>
678
+ : Routes & CreateClient<BasePath, NewSpiceflow['_routes']>
679
679
  >
680
680
  use<const Schema extends RouteSchema>(
681
681
  handler: MiddlewareHandler<
package/src/types.ts CHANGED
@@ -636,25 +636,25 @@ export type BaseMacro = Record<
636
636
  >
637
637
  export type BaseMacroFn = Record<string, (...a: any) => unknown>
638
638
 
639
- type _CreateEden<
639
+ type _CreateClient<
640
640
  Path extends string,
641
641
  Property extends Record<string, unknown> = {},
642
642
  > = Path extends `${infer Start}/${infer Rest}`
643
643
  ? {
644
- [x in Start]: _CreateEden<Rest, Property>
644
+ [x in Start]: _CreateClient<Rest, Property>
645
645
  }
646
646
  : {
647
647
  [x in Path]: Property
648
648
  }
649
649
 
650
- export type CreateEden<
650
+ export type CreateClient<
651
651
  Path extends string,
652
652
  Property extends Record<string, unknown> = {},
653
653
  > = Path extends `/${infer Rest}`
654
- ? _CreateEden<Rest, Property>
654
+ ? _CreateClient<Rest, Property>
655
655
  : Path extends ''
656
- ? _CreateEden<'index', Property>
657
- : _CreateEden<Path, Property>
656
+ ? _CreateClient<'index', Property>
657
+ : _CreateClient<Path, Property>
658
658
 
659
659
  export type ComposeSpiceflowResponse<Response, Handle> = Handle extends (
660
660
  ...a: any[]
package/src/utils.ts CHANGED
@@ -113,20 +113,20 @@ export function isResponse(result: any): result is Response {
113
113
  if (result instanceof Response) {
114
114
  return true
115
115
  }
116
- if (
117
- result &&
118
- typeof result === 'object' &&
119
- 'status' in result &&
120
- 'headers' in result &&
121
- 'body' in result
122
- ) {
123
- console.warn(
124
- 'spiceflow WARNING: you returned a Response that does not satisfy instanceof Response, probably because of some dumb polyfill\n',
125
- result,
126
- )
116
+ // if (
117
+ // result &&
118
+ // typeof result === 'object' &&
119
+ // 'status' in result &&
120
+ // 'headers' in result &&
121
+ // 'body' in result
122
+ // ) {
123
+ // console.warn(
124
+ // 'spiceflow WARNING: you returned a Response that does not satisfy instanceof Response, probably because of some dumb polyfill\n',
125
+ // result,
126
+ // )
127
127
 
128
- return true
129
- }
128
+ // return true
129
+ // }
130
130
 
131
131
  return false
132
132
  }
@@ -1,15 +0,0 @@
1
- import type { InputSchema } from '../types.js';
2
- import type { SpiceflowClient } from './types.js';
3
- export declare class EdenWS<in out Schema extends InputSchema<any> = {}> {
4
- url: string;
5
- ws: WebSocket;
6
- constructor(url: string);
7
- send(data: Schema['body'] | Schema['body'][]): this;
8
- on<K extends keyof WebSocketEventMap>(type: K, listener: (event: SpiceflowClient.WSEvent<K, Schema['response']>) => void, options?: boolean | AddEventListenerOptions): this;
9
- off<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): this;
10
- subscribe(onMessage: (event: SpiceflowClient.WSEvent<'message', Schema['response']>) => void, options?: boolean | AddEventListenerOptions): this;
11
- addEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (event: SpiceflowClient.WSEvent<K, Schema['response']>) => void, options?: boolean | AddEventListenerOptions): this;
12
- removeEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): this;
13
- close(): this;
14
- }
15
- //# sourceMappingURL=ws.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../src/client/ws.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAGjD,qBAAa,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,SAAS,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE;IAG1C,GAAG,EAAE,MAAM;IAF9B,EAAE,EAAE,SAAS,CAAA;gBAEM,GAAG,EAAE,MAAM;IAI9B,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE;IAc5C,EAAE,CAAC,CAAC,SAAS,MAAM,iBAAiB,EAClC,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,IAAI,EACzE,OAAO,CAAC,EAAE,OAAO,GAAG,uBAAuB;IAK7C,GAAG,CAAC,CAAC,SAAS,MAAM,iBAAiB,EACnC,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,GAAG,EAC5D,OAAO,CAAC,EAAE,OAAO,GAAG,oBAAoB;IAO1C,SAAS,CACP,SAAS,EAAE,CACT,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,KAC1D,IAAI,EACT,OAAO,CAAC,EAAE,OAAO,GAAG,uBAAuB;IAK7C,gBAAgB,CAAC,CAAC,SAAS,MAAM,iBAAiB,EAChD,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,IAAI,EACzE,OAAO,CAAC,EAAE,OAAO,GAAG,uBAAuB;IAoB7C,mBAAmB,CAAC,CAAC,SAAS,MAAM,iBAAiB,EACnD,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,GAAG,EAC5D,OAAO,CAAC,EAAE,OAAO,GAAG,oBAAoB;IAO1C,KAAK;CAKN"}
package/dist/client/ws.js DELETED
@@ -1,49 +0,0 @@
1
- import { parseStringifiedValue } from './utils.js';
2
- export class EdenWS {
3
- constructor(url) {
4
- this.url = url;
5
- this.ws = new WebSocket(url);
6
- }
7
- send(data) {
8
- if (Array.isArray(data)) {
9
- data.forEach((datum) => this.send(datum));
10
- return this;
11
- }
12
- this.ws.send(typeof data === 'object' ? JSON.stringify(data) : data.toString());
13
- return this;
14
- }
15
- on(type, listener, options) {
16
- return this.addEventListener(type, listener, options);
17
- }
18
- off(type, listener, options) {
19
- this.ws.removeEventListener(type, listener, options);
20
- return this;
21
- }
22
- subscribe(onMessage, options) {
23
- return this.addEventListener('message', onMessage, options);
24
- }
25
- addEventListener(type, listener, options) {
26
- this.ws.addEventListener(type, (ws) => {
27
- if (type === 'message') {
28
- const data = parseMessageEvent(ws);
29
- listener(Object.assign(Object.assign({}, ws), { data }));
30
- }
31
- else
32
- listener(ws);
33
- }, options);
34
- return this;
35
- }
36
- removeEventListener(type, listener, options) {
37
- this.off(type, listener, options);
38
- return this;
39
- }
40
- close() {
41
- this.ws.close();
42
- return this;
43
- }
44
- }
45
- const parseMessageEvent = (event) => {
46
- const messageString = event.data.toString();
47
- return messageString === 'null' ? null : parseStringifiedValue(messageString);
48
- };
49
- //# sourceMappingURL=ws.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ws.js","sourceRoot":"","sources":["../../src/client/ws.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAElD,MAAM,OAAO,MAAM;IAGjB,YAAmB,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;QAC5B,IAAI,CAAC,EAAE,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC,CAAA;IAC9B,CAAC;IAED,IAAI,CAAC,IAAuC;QAC1C,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACxB,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;YAEzC,OAAO,IAAI,CAAA;QACb,CAAC;QAED,IAAI,CAAC,EAAE,CAAC,IAAI,CACV,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAClE,CAAA;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAED,EAAE,CACA,IAAO,EACP,QAAyE,EACzE,OAA2C;QAE3C,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAA;IACvD,CAAC;IAED,GAAG,CACD,IAAO,EACP,QAA4D,EAC5D,OAAwC;QAExC,IAAI,CAAC,EAAE,CAAC,mBAAmB,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAA;QAEpD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,SAAS,CACP,SAES,EACT,OAA2C;QAE3C,OAAO,IAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;IAC7D,CAAC;IAED,gBAAgB,CACd,IAAO,EACP,QAAyE,EACzE,OAA2C;QAE3C,IAAI,CAAC,EAAE,CAAC,gBAAgB,CACtB,IAAI,EACJ,CAAC,EAAE,EAAE,EAAE;YACL,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACvB,MAAM,IAAI,GAAG,iBAAiB,CAAC,EAAkB,CAAC,CAAA;gBAElD,QAAQ,CAAC,gCACJ,EAAE,KACL,IAAI,GACE,CAAC,CAAA;YACX,CAAC;;gBAAM,QAAQ,CAAC,EAAS,CAAC,CAAA;QAC5B,CAAC,EACD,OAAO,CACR,CAAA;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAED,mBAAmB,CACjB,IAAO,EACP,QAA4D,EAC5D,OAAwC;QAExC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAA;QAEjC,OAAO,IAAI,CAAA;IACb,CAAC;IAED,KAAK;QACH,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAA;QAEf,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AAED,MAAM,iBAAiB,GAAG,CAAC,KAAmB,EAAE,EAAE;IAChD,MAAM,aAAa,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAA;IAE3C,OAAO,aAAa,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,qBAAqB,CAAC,aAAa,CAAC,CAAA;AAC/E,CAAC,CAAA"}
package/src/client/ws.ts DELETED
@@ -1,97 +0,0 @@
1
- import type { InputSchema } from '../types.js'
2
- import type { SpiceflowClient } from './types.js'
3
- import { parseStringifiedValue } from './utils.js'
4
-
5
- export class EdenWS<in out Schema extends InputSchema<any> = {}> {
6
- ws: WebSocket
7
-
8
- constructor(public url: string) {
9
- this.ws = new WebSocket(url)
10
- }
11
-
12
- send(data: Schema['body'] | Schema['body'][]) {
13
- if (Array.isArray(data)) {
14
- data.forEach((datum) => this.send(datum))
15
-
16
- return this
17
- }
18
-
19
- this.ws.send(
20
- typeof data === 'object' ? JSON.stringify(data) : data.toString(),
21
- )
22
-
23
- return this
24
- }
25
-
26
- on<K extends keyof WebSocketEventMap>(
27
- type: K,
28
- listener: (event: SpiceflowClient.WSEvent<K, Schema['response']>) => void,
29
- options?: boolean | AddEventListenerOptions,
30
- ) {
31
- return this.addEventListener(type, listener, options)
32
- }
33
-
34
- off<K extends keyof WebSocketEventMap>(
35
- type: K,
36
- listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any,
37
- options?: boolean | EventListenerOptions,
38
- ) {
39
- this.ws.removeEventListener(type, listener, options)
40
-
41
- return this
42
- }
43
-
44
- subscribe(
45
- onMessage: (
46
- event: SpiceflowClient.WSEvent<'message', Schema['response']>,
47
- ) => void,
48
- options?: boolean | AddEventListenerOptions,
49
- ) {
50
- return this.addEventListener('message', onMessage, options)
51
- }
52
-
53
- addEventListener<K extends keyof WebSocketEventMap>(
54
- type: K,
55
- listener: (event: SpiceflowClient.WSEvent<K, Schema['response']>) => void,
56
- options?: boolean | AddEventListenerOptions,
57
- ) {
58
- this.ws.addEventListener(
59
- type,
60
- (ws) => {
61
- if (type === 'message') {
62
- const data = parseMessageEvent(ws as MessageEvent)
63
-
64
- listener({
65
- ...ws,
66
- data,
67
- } as any)
68
- } else listener(ws as any)
69
- },
70
- options,
71
- )
72
-
73
- return this
74
- }
75
-
76
- removeEventListener<K extends keyof WebSocketEventMap>(
77
- type: K,
78
- listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any,
79
- options?: boolean | EventListenerOptions,
80
- ) {
81
- this.off(type, listener, options)
82
-
83
- return this
84
- }
85
-
86
- close() {
87
- this.ws.close()
88
-
89
- return this
90
- }
91
- }
92
-
93
- const parseMessageEvent = (event: MessageEvent) => {
94
- const messageString = event.data.toString()
95
-
96
- return messageString === 'null' ? null : parseStringifiedValue(messageString)
97
- }