@based/functions 1.0.1 → 1.0.2

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.
@@ -1,4 +1,4 @@
1
1
 
2
- > @based/functions@1.0.0 build
2
+ > @based/functions@1.0.1 build
3
3
  > npx tsc
4
4
 
package/dist/client.d.ts CHANGED
@@ -7,5 +7,5 @@ export declare abstract class BasedFunctionClient {
7
7
  abstract query(name: string, payload?: any): BasedQuery;
8
8
  abstract stream(name: string, stream?: any, ctx?: Context): Promise<any>;
9
9
  abstract sendAuthState(ctx: Context, authState: AuthState): void;
10
- abstract renewAuthState(ctx: Context, authState?: AuthState): void;
10
+ abstract renewAuthState(ctx: Context): void;
11
11
  }
package/dist/context.d.ts CHANGED
@@ -43,7 +43,7 @@ export declare type HttpSession = {
43
43
  export declare type InternalSessionObservable = {
44
44
  id: number;
45
45
  name: string;
46
- type: 'observable';
46
+ type: 'query';
47
47
  };
48
48
  export declare type InternalSessionClient = {
49
49
  client: BasedFunctionClient;
@@ -60,4 +60,5 @@ export declare type Context<S extends Session = Session> = {
60
60
  };
61
61
  export declare const isHttpContext: (ctx: Context<Session>) => ctx is Context<HttpSession>;
62
62
  export declare const isWsContext: (ctx: Context<Session>) => ctx is Context<WebSocketSession>;
63
+ export declare const isClientContext: (ctx: Context<Session>) => ctx is Context<WebSocketSession>;
63
64
  export declare const isWsSession: (session: Session) => session is WebSocketSession;
package/dist/context.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isWsSession = exports.isWsContext = exports.isHttpContext = void 0;
3
+ exports.isWsSession = exports.isClientContext = exports.isWsContext = exports.isHttpContext = void 0;
4
4
  const isHttpContext = (ctx) => {
5
5
  if ('res' in ctx?.session) {
6
6
  return true;
@@ -15,6 +15,13 @@ const isWsContext = (ctx) => {
15
15
  return false;
16
16
  };
17
17
  exports.isWsContext = isWsContext;
18
+ const isClientContext = (ctx) => {
19
+ if (ctx.session && 'authState' in ctx.session) {
20
+ return true;
21
+ }
22
+ return false;
23
+ };
24
+ exports.isClientContext = isClientContext;
18
25
  const isWsSession = (session) => {
19
26
  if (!('res' in session) && 'ip' in session && 'id' in session) {
20
27
  return true;
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":";;;AA4EO,MAAM,aAAa,GAAG,CAC3B,GAAqB,EACQ,EAAE;IAC/B,IAAI,KAAK,IAAI,GAAG,EAAE,OAAO,EAAE;QACzB,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAPY,QAAA,aAAa,iBAOzB;AAEM,MAAM,WAAW,GAAG,CACzB,GAAqB,EACa,EAAE;IACpC,IAAI,GAAG,CAAC,OAAO,IAAI,IAAA,mBAAW,EAAC,GAAG,CAAC,OAAO,CAAC,EAAE;QAC3C,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAPY,QAAA,WAAW,eAOvB;AAEM,MAAM,WAAW,GAAG,CAAC,OAAgB,EAA+B,EAAE;IAC3E,IAAI,CAAC,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,IAAI,IAAI,OAAO,IAAI,IAAI,IAAI,OAAO,EAAE;QAC7D,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AALY,QAAA,WAAW,eAKvB"}
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":";;;AA4EO,MAAM,aAAa,GAAG,CAC3B,GAAqB,EACQ,EAAE;IAC/B,IAAI,KAAK,IAAI,GAAG,EAAE,OAAO,EAAE;QACzB,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAPY,QAAA,aAAa,iBAOzB;AAEM,MAAM,WAAW,GAAG,CACzB,GAAqB,EACa,EAAE;IACpC,IAAI,GAAG,CAAC,OAAO,IAAI,IAAA,mBAAW,EAAC,GAAG,CAAC,OAAO,CAAC,EAAE;QAC3C,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAPY,QAAA,WAAW,eAOvB;AAEM,MAAM,eAAe,GAAG,CAC7B,GAAqB,EACa,EAAE;IACpC,IAAI,GAAG,CAAC,OAAO,IAAI,WAAW,IAAI,GAAG,CAAC,OAAO,EAAE;QAC7C,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAPY,QAAA,eAAe,mBAO3B;AAEM,MAAM,WAAW,GAAG,CAAC,OAAgB,EAA+B,EAAE;IAC3E,IAAI,CAAC,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,IAAI,IAAI,OAAO,IAAI,IAAI,IAAI,OAAO,EAAE;QAC7D,OAAO,IAAI,CAAA;KACZ;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AALY,QAAA,WAAW,eAKvB"}
@@ -7,7 +7,11 @@ export declare type ObservableUpdateFunction<K = any> = {
7
7
  __internalObs__?: true;
8
8
  };
9
9
  export declare type ObserveErrorListener = (err: any) => void;
10
- export declare type CustomHttpResponse = (result: any, payload: any, client: Context<HttpSession>) => Promise<boolean>;
10
+ export declare type HttpHeaders = {
11
+ [header: string]: number | string | (string | number)[];
12
+ };
13
+ export declare type SendHttpResponse = (responseData: any, headers?: HttpHeaders, status?: string | number) => void;
14
+ export declare type HttpResponse<P = any, K = any> = (based: BasedFunctionClient, payload: P, responseData: K, send: SendHttpResponse, ctx: Context<HttpSession>) => Promise<void>;
11
15
  export declare type BasedFunction<P = any, K = any> = (based: BasedFunctionClient, payload: P, ctx: Context) => Promise<K>;
12
16
  export declare type StreamPayload<P = any> = {
13
17
  payload?: P;
@@ -21,4 +25,4 @@ export declare type StreamPayload<P = any> = {
21
25
  extension?: string;
22
26
  };
23
27
  export declare type BasedStreamFunction<P = any, K = any> = BasedFunction<StreamPayload<P>, K>;
24
- export declare type BasedQueryFunction<P = any, K = any> = ((based: BasedFunctionClient, payload: P, update: ObservableUpdateFunction<K>) => Promise<() => void>) | ((based: BasedFunctionClient, payload: P, update: ObservableUpdateFunction<K>) => () => void);
28
+ export declare type BasedQueryFunction<P = any, K = any> = ((based: BasedFunctionClient, payload: P, update: ObservableUpdateFunction<K>, error?: ObserveErrorListener) => Promise<() => void>) | ((based: BasedFunctionClient, payload: P, update: ObservableUpdateFunction<K>, error?: ObserveErrorListener) => () => void);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/functions",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/client.ts CHANGED
@@ -13,5 +13,5 @@ export abstract class BasedFunctionClient {
13
13
 
14
14
  abstract sendAuthState(ctx: Context, authState: AuthState): void
15
15
 
16
- abstract renewAuthState(ctx: Context, authState?: AuthState): void
16
+ abstract renewAuthState(ctx: Context): void
17
17
  }
package/src/context.ts CHANGED
@@ -47,7 +47,7 @@ export type HttpSession = {
47
47
  export type InternalSessionObservable = {
48
48
  id: number
49
49
  name: string
50
- type: 'observable'
50
+ type: 'query'
51
51
  }
52
52
 
53
53
  export type InternalSessionClient = {
@@ -92,6 +92,15 @@ export const isWsContext = (
92
92
  return false
93
93
  }
94
94
 
95
+ export const isClientContext = (
96
+ ctx: Context<Session>
97
+ ): ctx is Context<WebSocketSession> => {
98
+ if (ctx.session && 'authState' in ctx.session) {
99
+ return true
100
+ }
101
+ return false
102
+ }
103
+
95
104
  export const isWsSession = (session: Session): session is WebSocketSession => {
96
105
  if (!('res' in session) && 'ip' in session && 'id' in session) {
97
106
  return true
package/src/functions.ts CHANGED
@@ -19,11 +19,23 @@ export type ObservableUpdateFunction<K = any> = {
19
19
  // TODO: use error package
20
20
  export type ObserveErrorListener = (err: any) => void
21
21
 
22
- export type CustomHttpResponse = (
23
- result: any,
24
- payload: any,
25
- client: Context<HttpSession>
26
- ) => Promise<boolean>
22
+ export type HttpHeaders = {
23
+ [header: string]: number | string | (string | number)[]
24
+ }
25
+
26
+ export type SendHttpResponse = (
27
+ responseData: any,
28
+ headers?: HttpHeaders,
29
+ status?: string | number
30
+ ) => void
31
+
32
+ export type HttpResponse<P = any, K = any> = (
33
+ based: BasedFunctionClient,
34
+ payload: P,
35
+ responseData: K,
36
+ send: SendHttpResponse,
37
+ ctx: Context<HttpSession>
38
+ ) => Promise<void>
27
39
 
28
40
  export type BasedFunction<P = any, K = any> = (
29
41
  based: BasedFunctionClient,
@@ -52,10 +64,12 @@ export type BasedQueryFunction<P = any, K = any> =
52
64
  | ((
53
65
  based: BasedFunctionClient,
54
66
  payload: P,
55
- update: ObservableUpdateFunction<K>
67
+ update: ObservableUpdateFunction<K>,
68
+ error?: ObserveErrorListener
56
69
  ) => Promise<() => void>)
57
70
  | ((
58
71
  based: BasedFunctionClient,
59
72
  payload: P,
60
- update: ObservableUpdateFunction<K>
73
+ update: ObservableUpdateFunction<K>,
74
+ error?: ObserveErrorListener
61
75
  ) => () => void)