@based/functions 3.0.2 → 3.1.0-alpha

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/auth.d.ts CHANGED
@@ -8,6 +8,7 @@ export type AuthState = {
8
8
  error?: string;
9
9
  persistent?: boolean;
10
10
  type?: string;
11
+ t?: 0 | 1;
11
12
  };
12
13
  export type Authorize = (based: BasedFunctionClient, context: Context<HttpSession | WebSocketSession>, name: string, // name as generic dope
13
14
  payload?: any) => Promise<boolean>;
package/dist/context.d.ts CHANGED
@@ -11,6 +11,7 @@ export type WebSocketSession = {
11
11
  id: number;
12
12
  method: string;
13
13
  authState: AuthState;
14
+ type: '0' | '1';
14
15
  origin: string;
15
16
  obs: Set<number>;
16
17
  unauthorizedObs?: Set<{
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AA6GA,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,GAAqB,EACQ,EAAE;IAC/B,IAAI,KAAK,IAAI,GAAG,EAAE,OAAO,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,GAAqB,EACa,EAAE;IACpC,IAAI,GAAG,CAAC,OAAO,IAAI,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5C,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,GAAqB,EAC2B,EAAE;IAClD,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;QAC5E,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO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,IAAI,OAAO,IAAI,KAAK,IAAI,OAAO,EAAE,CAAC;QAChC,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,OAA4B,EACC,EAAE;IAC/B,IAAI,OAAO,IAAI,MAAM,IAAI,OAAO,EAAE,CAAC;QACjC,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA"}
@@ -3,6 +3,7 @@ import { Context, HttpSession } from './context.js';
3
3
  import { BasedFunctionClient } from './client.js';
4
4
  import { BasedDataStream } from './stream.js';
5
5
  import { Authorize } from './auth.js';
6
+ import type { BuildOptions } from 'esbuild';
6
7
  export type ObservableUpdateFunction<K = any> = (data: K, checksum?: number, err?: any, cache?: Uint8Array, diff?: any, fromChecksum?: number, isDeflate?: boolean) => void;
7
8
  export type ObserveErrorListener = (err: any) => void;
8
9
  export type HttpHeaders = {
@@ -160,6 +161,13 @@ type BasedAppFunctionConfig = {
160
161
  main: string;
161
162
  path?: string;
162
163
  favicon?: string;
164
+ build?: {
165
+ watch?: {
166
+ include: string[];
167
+ exclude?: string[];
168
+ };
169
+ plugins?: BuildOptions['plugins'];
170
+ };
163
171
  };
164
172
  type BasedJobFunctionConfig = {
165
173
  type: 'job';
@@ -1 +1 @@
1
- {"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":"AAmUA,MAAM,UAAU,YAAY,CAC1B,IAAO,EACP,KAAU;IAEV,OAAO,CACL,KAAK;QACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,CAAC,IAAI,KAAK,IAAI;QACnB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAC/B,CAAA;AACH,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAAU;IACxC,OAAO,CACL,KAAK;QACL,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAC,KAAK,CAAC,IA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1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="node" resolution-mode="require"/>
3
- /// <reference types="node" resolution-mode="require"/>
4
- /// <reference types="node" resolution-mode="require"/>
5
1
  import { Duplex, Readable } from "node:stream";
6
2
  import util from "node:util";
7
3
  export declare class BasedDataStream extends Duplex {
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@based/functions",
3
- "version": "3.0.2",
3
+ "version": "3.1.0-alpha",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
+ "files":[
8
+ "dist"
9
+ ],
7
10
  "scripts": {
8
11
  "build": "npx tsc && cp src/uws.d.ts dist/uws.d.ts && cp ./dist/client.d.ts ./dist/client_original.d.ts",
9
12
  "watch": "npx tsc --watch",
@@ -11,12 +14,13 @@
11
14
  },
12
15
  "sideEffects": false,
13
16
  "dependencies": {
14
- "utility-types": "^3.10.0",
15
- "@saulx/utils": "^5.0.0"
17
+ "@saulx/utils": "^5.0.0",
18
+ "esbuild": "^0.24.2",
19
+ "utility-types": "^3.10.0"
16
20
  },
17
21
  "devDependencies": {
22
+ "rimraf": "^3.0.2",
18
23
  "ts-node": "10.9.1",
19
- "typescript": "^4.3.5",
20
- "rimraf": "^3.0.2"
24
+ "typescript": "^4.3.5"
21
25
  }
22
26
  }
package/src/auth.ts DELETED
@@ -1,34 +0,0 @@
1
- import { BasedFunctionClient } from './client.js'
2
- import { HttpRequest } from './uws.js'
3
- import { Context, WebSocketSession, HttpSession } from './context.js'
4
-
5
- export type AuthState = {
6
- token?: string
7
- userId?: string
8
- refreshToken?: string
9
- error?: string
10
- persistent?: boolean
11
- type?: string
12
- }
13
-
14
- export type Authorize = (
15
- based: BasedFunctionClient,
16
- context: Context<HttpSession | WebSocketSession>,
17
- name: string, // name as generic dope
18
- payload?: any
19
- ) => Promise<boolean>
20
-
21
- // True - its the same all good
22
- // AuthState - new auth state send it
23
- // if error send error state (and reject)
24
- export type VerifyAuthState = (
25
- based: BasedFunctionClient,
26
- context: Context<HttpSession | WebSocketSession>,
27
- authState: AuthState
28
- ) => Promise<true | AuthState>
29
-
30
- export type AuthorizeConnection = (
31
- based: BasedFunctionClient,
32
- req: HttpRequest,
33
- ip: string
34
- ) => Promise<boolean>
package/src/channel.ts DELETED
@@ -1,10 +0,0 @@
1
- import { ChannelMessageFunction } from './functions.js'
2
-
3
- export abstract class BasedChannel<K = any> {
4
- abstract subscribe(
5
- onMessage: ChannelMessageFunction<K>,
6
- onError?: (err: any) => void
7
- ): () => void
8
-
9
- abstract publish(message: K): void
10
- }
package/src/client.ts DELETED
@@ -1,35 +0,0 @@
1
- import { AuthState } from './auth.js'
2
- import { BasedChannel } from './channel.js'
3
- import { Context } from './context.js'
4
- import { BasedQuery } from './query.js'
5
- import { StreamFunctionOpts } from './stream.js'
6
- import { Geo } from './geo.js'
7
-
8
- export abstract class BasedFunctionClient {
9
- server: any
10
-
11
- db: any
12
-
13
- abstract call(name: string, payload?: any, ctx?: Context): Promise<any>
14
-
15
- abstract query(name: string, payload?: any): BasedQuery
16
-
17
- abstract channel(name: string, payload?: any): BasedChannel
18
-
19
- abstract stream(
20
- name: string,
21
- payload: StreamFunctionOpts,
22
- ctx?: Context
23
- ): Promise<any>
24
-
25
- abstract sendAuthState(ctx: Context, authState: AuthState): void
26
-
27
- abstract geo(ctx: Context): Promise<Geo>
28
-
29
- abstract renewAuthState(
30
- ctx: Context,
31
- authState?: AuthState
32
- ): Promise<AuthState>
33
- }
34
-
35
- export type QueryMap = { [key: string]: { payload: any; result: any } }
package/src/context.ts DELETED
@@ -1,153 +0,0 @@
1
- import { parseQuery } from '@saulx/utils'
2
- import { AuthState } from './auth.js'
3
- import { WebSocket, HttpRequest, HttpResponse } from './uws.js'
4
- import { BasedFunctionClient } from './client.js'
5
- import { StreamPayload } from './functions.js'
6
-
7
- export type WebSocketSession = {
8
- // State can be used for anything - for us the based class instance
9
- state?: any
10
- query: string
11
- ua: string
12
- ip: string
13
- id: number // client-id
14
- method: string
15
- authState: AuthState
16
- origin: string
17
- obs: Set<number>
18
- unauthorizedObs?: Set<{
19
- id: number
20
- checksum: number
21
- name: string
22
- payload: any
23
- }>
24
- streams?: { [reqId: string]: StreamPayload }
25
- headers: { [key: string]: string }
26
- unauthorizedChannels?: Set<{
27
- id: number
28
- name: string
29
- payload: any
30
- }>
31
- // Optimization so we dont need to keep track of websockets outside of uws
32
- c?: Context<WebSocketSession>
33
- ws?: BasedWebSocket
34
- }
35
-
36
- export type BasedWebSocket = WebSocket<WebSocketSession>
37
-
38
- export type HttpSession = {
39
- // State can be used for anything - for us the based class instance
40
- state?: any
41
- res: HttpResponse
42
- url: string
43
- origin: string
44
- req: HttpRequest
45
- query?: string
46
- parsedQuery?: ReturnType<typeof parseQuery>
47
- ua: string
48
- ip: string
49
- id: number // client-id
50
- authState: AuthState
51
- method: string
52
- rawBody?: string
53
- headers: {
54
- 'content-length'?: number
55
- 'content-type'?: string
56
- 'content-encoding'?: string
57
- encoding?: string
58
- } & { [key: string]: string }
59
- }
60
-
61
- export type InternalSessionObservable = {
62
- id: number
63
- name: string
64
- headers: { [key: string]: string }
65
- type: 'query'
66
- }
67
-
68
- export type InternalSessionChannel = {
69
- id: number
70
- name: string
71
- headers: { [key: string]: string }
72
- type: 'channel'
73
- }
74
-
75
- export type InternalSessionClient = {
76
- client: BasedFunctionClient
77
- headers: { [key: string]: string }
78
- type: 'client'
79
- }
80
-
81
- // Internal session for internal functions
82
- export type InternalSession =
83
- | InternalSessionClient
84
- | InternalSessionObservable
85
- | InternalSessionChannel
86
-
87
- // used for minimal security errors (e.g. rate limit)
88
- export type MinimalExternalSession = {
89
- ua: string
90
- ip: string
91
- headers: { [key: string]: string }
92
- }
93
-
94
- export type Session = (
95
- | WebSocketSession
96
- | HttpSession
97
- | InternalSession
98
- | MinimalExternalSession
99
- ) & {
100
- origin?: string
101
-
102
- /** Only available in Ws and Http contexts */
103
- authState?: AuthState
104
- }
105
-
106
- export type Context<S extends Session = Session> = {
107
- session?: S
108
- }
109
-
110
- export const isHttpContext = (
111
- ctx: Context<Session>
112
- ): ctx is Context<HttpSession> => {
113
- if ('res' in ctx?.session) {
114
- return true
115
- }
116
- return false
117
- }
118
-
119
- export const isWsContext = (
120
- ctx: Context<Session>
121
- ): ctx is Context<WebSocketSession> => {
122
- if (ctx.session && isWsSession(ctx.session)) {
123
- return true
124
- }
125
- return false
126
- }
127
-
128
- export const isClientContext = (
129
- ctx: Context<Session>
130
- ): ctx is Context<WebSocketSession | HttpSession> => {
131
- if (ctx.session && (isWsSession(ctx.session) || isHttpSession(ctx.session))) {
132
- return true
133
- }
134
- return false
135
- }
136
-
137
- export const isHttpSession = (
138
- session: Session | undefined
139
- ): session is HttpSession => {
140
- if (session && 'res' in session) {
141
- return true
142
- }
143
- return false
144
- }
145
-
146
- export const isWsSession = (
147
- session: Session | undefined
148
- ): session is WebSocketSession => {
149
- if (session && 'send' in session) {
150
- return true
151
- }
152
- return false
153
- }
package/src/functions.ts DELETED
@@ -1,367 +0,0 @@
1
- import type { Required } from 'utility-types'
2
- import { Context, HttpSession } from './context.js'
3
- import { BasedFunctionClient } from './client.js'
4
- import { BasedDataStream } from './stream.js'
5
- import { Authorize } from './auth.js'
6
-
7
- export type ObservableUpdateFunction<K = any> = (
8
- data: K,
9
- checksum?: number,
10
- err?: any,
11
- cache?: Uint8Array,
12
- diff?: any,
13
- fromChecksum?: number,
14
- isDeflate?: boolean
15
- ) => void
16
-
17
- export type ObserveErrorListener = (err: any) => void
18
-
19
- export type HttpHeaders = {
20
- [header: string]: number | string | (string | number)[]
21
- }
22
-
23
- export type SendHttpResponse = (
24
- responseData: any,
25
- headers?: HttpHeaders,
26
- status?: string | number
27
- ) => void
28
-
29
- export type HttpResponse<P = any, K = any> = (
30
- based: BasedFunctionClient,
31
- payload: P,
32
- responseData: K,
33
- send: SendHttpResponse,
34
- ctx: Context<HttpSession>
35
- ) => Promise<void>
36
-
37
- export type BasedFunction<P = any, K = any> = (
38
- based: BasedFunctionClient,
39
- payload: P,
40
- ctx: Context
41
- ) => Promise<K>
42
-
43
- export type BasedAppFunction = (
44
- based: BasedFunctionClient,
45
- assets: {
46
- css: {
47
- text: Promise<string>
48
- url: string
49
- }
50
- js: {
51
- text: Promise<string>
52
- url: string
53
- }
54
- favicon: {
55
- url: string
56
- content: string
57
- path: string
58
- }
59
- },
60
- ctx: Context
61
- ) => Promise<string>
62
-
63
- export type StreamPayload<P = any> = {
64
- payload?: P
65
- mimeType: string
66
- size: number
67
- stream: BasedDataStream
68
- fileName?: string
69
- extension?: string
70
- fn?: string
71
- seqId?: number
72
- }
73
-
74
- export type BasedStreamFunction<P = any, K = any> = BasedFunction<
75
- StreamPayload<P>,
76
- K
77
- >
78
-
79
- export type BasedQueryFunction<P = any, K = any> =
80
- | ((
81
- based: BasedFunctionClient,
82
- payload: P,
83
- update: ObservableUpdateFunction<K>,
84
- error: ObserveErrorListener
85
- ) => Promise<() => void>)
86
- | ((
87
- based: BasedFunctionClient,
88
- payload: P,
89
- update: ObservableUpdateFunction<K>,
90
- error: ObserveErrorListener
91
- ) => () => void)
92
-
93
- export type BasedChannelFunction<P = any, K = any> = (
94
- based: BasedFunctionClient,
95
- payload: P,
96
- id: number,
97
- update: ChannelMessageFunction<K>,
98
- error: (err?: any) => void
99
- ) => () => void
100
-
101
- export type BasedChannelPublishFunction<P = any, M = any> = (
102
- based: BasedFunctionClient,
103
- payload: P,
104
- message: M,
105
- id: number,
106
- ctx: Context
107
- ) => void
108
-
109
- export type ChannelMessageFunction<M = any> = (message: M) => void
110
-
111
- export type ChannelMessageFunctionInternal<K = any> = (
112
- message: K,
113
- err?: any
114
- ) => void
115
-
116
- export type BasedJobFunction =
117
- | ((based: BasedFunctionClient) => Promise<() => void>)
118
- | ((based: BasedFunctionClient) => () => void)
119
-
120
- export type UninstallFunction = () => Promise<void>
121
-
122
- // ------------ Config -------------------
123
- type FunctionConfigShared = {
124
- /** Function name */
125
- name?: string
126
- /** In addition to the name, a function can have a custom path for HTTP requests.
127
- * For example: `path: 'my/custom/path'` will result in the function being
128
- * available with a request to `env.based.io/my/custom/path`
129
- */
130
- path?: string
131
- /** In bytes. `-1` indicates no size limit */
132
- maxPayloadSize?: number
133
- /** Cost in tokens for this function call. */
134
- rateLimitTokens?: number
135
- /** A function marked as `public` will skip the call to authorize. */
136
- public?: boolean
137
- /** Array of headers that this function expects to receive. */
138
- headers?: string[]
139
- /** A function marked as `internalOnly` will only be accessible from other server side functions,
140
- * and not through the public internet.
141
- */
142
- internalOnly?: boolean
143
- /** Can hold extra information about a spec */
144
- data?: any
145
- /** Unistall after idle, in ms -1 indicates endless */
146
- uninstallAfterIdleTime?: number
147
- /** Hook that fires on uninstall of the function e.g. to clean up database connections */
148
- uninstall?: UninstallFunction
149
- /** Specific authorize for this function */
150
- authorize?: Authorize
151
- /** Relay allows functions to relay traffic to another `@based/server`
152
- `Currently not supported for `stream`
153
-
154
- ```js
155
- new BasedServer({
156
- clients: { events: BasedClient },
157
- functions: {
158
- specs:
159
- somethingToRelay: {
160
- relay: { client: 'events', target: 'hello' },
161
- type: 'function'
162
- })
163
- }
164
- }
165
- })
166
- ```
167
- */
168
- relay?: {
169
- client: string
170
- target?: string
171
- }
172
- /** Function version */
173
- version?: number
174
- /** Used inernaly to check if a function is ready to uninstall */
175
- timeoutCounter?: number
176
- }
177
-
178
- type FunctionConfigSharedComplete = Required<
179
- FunctionConfigShared,
180
- 'maxPayloadSize' | 'rateLimitTokens' | 'version' | 'name'
181
- >
182
-
183
- export type BasedFunctionTypes =
184
- | 'channel'
185
- | 'query'
186
- | 'function'
187
- | 'stream'
188
- | 'app'
189
- | 'job'
190
-
191
- type BasedChannelFunctionConfig = {
192
- /** Function type `channel, function, query, stream, authorize` */
193
- type: 'channel'
194
- /** Channel subscriber
195
-
196
- ```js
197
- const subscribe = (based, payload, id, update) => {
198
- let cnt = 0
199
- const interval = setInterval(() => {
200
- update(++cnt)
201
- })
202
- return () => clearInterval(cnt)
203
- }
204
- ```
205
- */
206
- subscriber?: BasedChannelFunction
207
- /** Channel publisher
208
-
209
- ```js
210
- const publisher = (based, payload, msg, id) => {
211
- publishToChannel(id, msg)
212
- }
213
- ```
214
- */
215
- publisher?: BasedChannelPublishFunction
216
- /** Makes only the publisher public */
217
- publicPublisher?: boolean
218
- /** How long should the channel subscriber remain active after all subscribers are gone, in ms */
219
- closeAfterIdleTime?: number
220
- /** Only for Publisher */
221
- httpResponse?: HttpResponse
222
- /** Throttle amount of outgoing messages, in milliseconds */
223
- throttle?: number
224
- }
225
-
226
- type BasedCallFunctionConfig = {
227
- /** Function type `channel, function, query, stream` */
228
- type: 'function'
229
- fn?: BasedFunction
230
- httpResponse?: HttpResponse
231
- }
232
-
233
- type BasedQueryFunctionConfig = {
234
- /** Function type `channel, function, query, stream` */
235
- type: 'query'
236
- fn?: BasedQueryFunction
237
- httpResponse?: HttpResponse
238
- /** How long should the query function remain active after all subscribers are gone, in ms */
239
- closeAfterIdleTime?: number
240
- /** Throttle amount of outgoing messages, in milliseconds */
241
- throttle?: number
242
- }
243
-
244
- type BasedStreamFunctionConfig = {
245
- /** Function type `channel, function, query, stream` */
246
- type: 'stream'
247
- fn: BasedStreamFunction
248
- }
249
-
250
- type BasedAppFunctionConfig = {
251
- type: 'app'
252
- main: string
253
- path?: string
254
- favicon?: string
255
- }
256
-
257
- type BasedJobFunctionConfig = {
258
- type: 'job'
259
- fn?: BasedFunction
260
- }
261
-
262
- export type BasedFunctionConfig<
263
- T extends BasedFunctionTypes = BasedFunctionTypes
264
- > = T extends 'channel'
265
- ? BasedChannelFunctionConfig & FunctionConfigShared
266
- : T extends 'function'
267
- ? BasedCallFunctionConfig & FunctionConfigShared
268
- : T extends 'query'
269
- ? BasedQueryFunctionConfig & FunctionConfigShared
270
- : T extends 'stream'
271
- ? BasedStreamFunctionConfig & FunctionConfigShared
272
- : T extends 'job'
273
- ? BasedJobFunctionConfig & FunctionConfigShared
274
- : T extends 'app'
275
- ? BasedAppFunctionConfig & FunctionConfigShared
276
- :
277
- | (BasedChannelFunctionConfig & FunctionConfigShared)
278
- | (BasedCallFunctionConfig & FunctionConfigShared)
279
- | (BasedQueryFunctionConfig & FunctionConfigShared)
280
- | (BasedStreamFunctionConfig & FunctionConfigShared)
281
- | (BasedJobFunctionConfig & FunctionConfigShared)
282
- | (BasedAppFunctionConfig & FunctionConfigShared)
283
-
284
- export type BasedFunctionConfigComplete<
285
- T extends BasedFunctionTypes = BasedFunctionTypes
286
- > = T extends 'channel'
287
- ? BasedChannelFunctionConfig & FunctionConfigSharedComplete
288
- : T extends 'function'
289
- ? BasedCallFunctionConfig & FunctionConfigSharedComplete
290
- : T extends 'query'
291
- ? BasedQueryFunctionConfig & FunctionConfigSharedComplete
292
- : T extends 'stream'
293
- ? BasedStreamFunctionConfig & FunctionConfigSharedComplete
294
- : T extends 'job'
295
- ? BasedJobFunctionConfig & FunctionConfigSharedComplete
296
- : T extends 'app'
297
- ? BasedAppFunctionConfig & FunctionConfigSharedComplete
298
- :
299
- | (BasedChannelFunctionConfig & FunctionConfigSharedComplete)
300
- | (BasedCallFunctionConfig & FunctionConfigSharedComplete)
301
- | (BasedQueryFunctionConfig & FunctionConfigSharedComplete)
302
- | (BasedStreamFunctionConfig & FunctionConfigSharedComplete)
303
- | (BasedJobFunctionConfig & FunctionConfigSharedComplete)
304
- | (BasedAppFunctionConfig & FunctionConfigSharedComplete)
305
-
306
- export type BasedAuthorizeFunctionConfig = {
307
- /** Function type `authorize` */
308
- type: 'authorize'
309
- fn?: Authorize
310
- }
311
-
312
- export type BasedRoute<
313
- T extends BasedFunctionTypes = BasedFunctionTypes,
314
- R extends keyof BasedFunctionConfig = 'type' | 'name'
315
- > = Required<Partial<BasedFunctionConfig<T>>, R>
316
-
317
- export type BasedRouteComplete<
318
- T extends BasedFunctionTypes = BasedFunctionTypes
319
- > = Required<
320
- Partial<BasedFunctionConfig<T>>,
321
- 'type' | 'name' | 'maxPayloadSize' | 'rateLimitTokens'
322
- >
323
-
324
- export function isBasedRoute<T extends BasedFunctionTypes>(
325
- type: T,
326
- route: any
327
- ): route is BasedRoute<T> {
328
- return (
329
- route &&
330
- typeof route === 'object' &&
331
- route.type === type &&
332
- typeof route.name === 'string'
333
- )
334
- }
335
-
336
- export function isAnyBasedRoute(route: any): route is BasedRoute {
337
- return (
338
- route &&
339
- typeof route === 'object' &&
340
- (route.type === 'channel' ||
341
- route.type === 'query' ||
342
- route.type === 'function' ||
343
- route.type === 'stream') &&
344
- typeof route.name === 'string'
345
- )
346
- }
347
-
348
- export function isBasedFunctionConfig<T extends BasedFunctionTypes>(
349
- type: T,
350
- config: any
351
- ): config is BasedFunctionConfig<T> {
352
- return isBasedRoute(type, config)
353
- }
354
-
355
- export function isAnyBasedFunctionConfig(
356
- config: any
357
- ): config is BasedFunctionConfig {
358
- return isAnyBasedRoute(config)
359
- }
360
-
361
- export type BasedRoutes = {
362
- [name: string]: BasedRoute<BasedFunctionTypes, 'type'>
363
- }
364
-
365
- export type BasedFunctionConfigs = {
366
- [name: string]: BasedFunctionConfig<BasedFunctionTypes>
367
- }