oc-server 1.1.11 → 2.0.1

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/Server.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Prettify, ToJson, DataContext, DataProvider } from './types';
1
+ import { Prettify, DataContext, DataProvider, ToPrettyJson } from './types';
2
2
  export type ServerContext<E = {
3
3
  name: string;
4
4
  }, P = any, S = any> = Omit<DataContext<any, E, P, S>, 'params' | 'action' | 'setEmptyResponse'>;
@@ -34,25 +34,30 @@ type TransformOcParameters<T extends OcParameters> = Prettify<Pick<{
34
34
  }, {
35
35
  [K in keyof T]: T[K]['mandatory'] extends true ? never : K;
36
36
  }[keyof T]>>>;
37
+ interface ServerOptions<S extends boolean> {
38
+ stream?: S;
39
+ }
37
40
  export declare class Server<E = {
38
41
  name: string;
39
- }, P = unknown, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any, MiddlewareInput = any, MiddlewareOutput = any> {
42
+ }, P = unknown, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any, MiddlewareInput = any, MiddlewareOutput = any, Streaming extends boolean = false> {
43
+ private readonly _options;
40
44
  private _parameters;
41
45
  private _middleware;
42
- constructor();
43
- middleware<I = MiddlewareInput, O = MiddlewareOutput>(action: Action<I, O, E, P, unknown>): Omit<Server<E, P, A, InitialInput, InitialOutput, I, O>, P extends Record<string, unknown> ? IsGeneralStringName<E> extends false ? 'middleware' | 'definePlugins' | 'defineEnv' : 'middleware' | 'definePlugins' : IsGeneralStringName<E> extends false ? 'middleware' | 'defineEnv' : 'middleware'>;
44
- withParameters<T extends OcParameters>(params: T): Server<E, P, A, TransformOcParameters<T>, InitialOutput, TransformOcParameters<T>, MiddlewareOutput>;
45
- defineEnv<Env>(): P extends Record<string, any> ? Omit<Server<Env, P, A, InitialInput, InitialOutput>, 'defineEnv' | 'definePlugins'> : Omit<Server<Env, P, A, InitialInput, InitialOutput>, 'defineEnv'>;
46
- definePlugins<Plugins>(): IsGeneralStringName<E> extends true ? Omit<Server<E, Plugins, A, InitialInput, InitialOutput>, 'definePlugins'> : Omit<Server<E, Plugins, A, InitialInput, InitialOutput>, 'definePlugins' | 'defineEnv'>;
47
- handler<I = InitialInput, O = InitialOutput>(action: Action<I, O, E, P, MiddlewareOutput>): HandledServer<E, P, A, I, O, MiddlewareInput, MiddlewareOutput>;
46
+ constructor(_options?: ServerOptions<Streaming>);
47
+ middleware<I = MiddlewareInput, O = MiddlewareOutput>(action: Action<I, O, E, P, unknown>): Omit<Server<E, P, A, InitialInput, InitialOutput, I, O, Streaming>, P extends Record<string, unknown> ? IsGeneralStringName<E> extends false ? 'middleware' | 'definePlugins' | 'defineEnv' : 'middleware' | 'definePlugins' : IsGeneralStringName<E> extends false ? 'middleware' | 'defineEnv' : 'middleware'>;
48
+ withParameters<T extends OcParameters>(params: T): Server<E, P, A, TransformOcParameters<T>, InitialOutput, TransformOcParameters<T>, MiddlewareOutput, Streaming>;
49
+ defineEnv<Env>(): P extends Record<string, any> ? Omit<Server<Env, P, A, InitialInput, InitialOutput, MiddlewareInput, MiddlewareOutput, Streaming>, 'defineEnv' | 'definePlugins'> : Omit<Server<Env, P, A, InitialInput, InitialOutput, MiddlewareInput, MiddlewareOutput, Streaming>, 'defineEnv'>;
50
+ definePlugins<Plugins>(): IsGeneralStringName<E> extends true ? Omit<Server<E, Plugins, A, InitialInput, InitialOutput, MiddlewareInput, MiddlewareOutput, Streaming>, 'definePlugins'> : Omit<Server<E, Plugins, A, InitialInput, InitialOutput, MiddlewareInput, MiddlewareOutput, Streaming>, 'definePlugins' | 'defineEnv'>;
51
+ handler<I = InitialInput, O = InitialOutput>(action: Action<I, O, E, P, MiddlewareOutput>): HandledServer<E, P, A, I, O, MiddlewareInput, MiddlewareOutput, Streaming>;
48
52
  }
49
- declare class HandledServer<E, P, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any, MiddlewareInput = any, MiddlewareOutput = any> {
53
+ declare class HandledServer<E, P, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any, MiddlewareInput = any, MiddlewareOutput = any, Streaming extends boolean = false> {
50
54
  readonly initial: Action<InitialInput, InitialOutput, E, P, MiddlewareOutput>;
51
55
  private readonly _middleware;
52
56
  private readonly _parameters;
57
+ private readonly _options;
53
58
  readonly actions: A;
54
- constructor(initial: Action<InitialInput, InitialOutput, E, P, MiddlewareOutput>, _middleware?: Action<MiddlewareInput, MiddlewareOutput, E, P, unknown> | null, _parameters?: OcParameters);
55
- action<ActionName extends string, I, O>(name: ActionName, action: Action<I, O, E, P, MiddlewareOutput>): HandledServer<E, P, Prettify<A & Record<ActionName, Action<I, O, E, P, MiddlewareOutput>>>, InitialInput, InitialOutput, MiddlewareInput, MiddlewareOutput>;
59
+ constructor(initial: Action<InitialInput, InitialOutput, E, P, MiddlewareOutput>, _middleware?: Action<MiddlewareInput, MiddlewareOutput, E, P, unknown> | null, _parameters?: OcParameters, _options?: ServerOptions<Streaming>);
60
+ action<ActionName extends string, I, O>(name: ActionName, action: Action<I, O, E, P, MiddlewareOutput>): HandledServer<E, P, Prettify<A & Record<ActionName, Action<I, O, E, P, MiddlewareOutput>>>, InitialInput, InitialOutput, MiddlewareInput, MiddlewareOutput, Streaming>;
56
61
  getData(): DataProvider<any, any, any>;
57
62
  }
58
63
  export interface Register {
@@ -62,7 +67,7 @@ export type RegisteredServer = Register extends {
62
67
  server: infer TServer extends AnyServer;
63
68
  } ? TServer : AnyServer;
64
69
  export type GetMiddlewareInput<TServer extends AnyServer> = TServer extends HandledServer<any, any, any, any, any, infer I> ? I : any;
65
- type GetInitialData<TServer extends AnyServer> = TServer extends HandledServer<any, any, any, any, infer O> ? Exclude<ToJson<O>, undefined | null> : any;
70
+ type GetInitialData<TServer extends AnyServer> = TServer extends HandledServer<any, any, any, any, infer O, any, any, infer Streaming> ? Streaming extends true ? Exclude<O, undefined | null> : Exclude<ToPrettyJson<O>, undefined | null> : any;
66
71
  export type InitialData = GetInitialData<RegisteredServer>;
67
72
  export type ComponentSettings = {
68
73
  id: string;
package/dist/Server.js CHANGED
@@ -2,9 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ServerError = exports.getSettings = exports.getInitialData = exports.Server = void 0;
4
4
  class Server {
5
+ _options;
5
6
  _parameters = {};
6
7
  _middleware = null;
7
- constructor() { }
8
+ constructor(_options = {}) {
9
+ this._options = _options;
10
+ }
8
11
  middleware(action) {
9
12
  this._middleware = action;
10
13
  return this;
@@ -20,7 +23,7 @@ class Server {
20
23
  return this;
21
24
  }
22
25
  handler(action) {
23
- return new HandledServer(action, this._middleware, this._parameters);
26
+ return new HandledServer(action, this._middleware, this._parameters, this._options);
24
27
  }
25
28
  }
26
29
  exports.Server = Server;
@@ -28,11 +31,13 @@ class HandledServer {
28
31
  initial;
29
32
  _middleware;
30
33
  _parameters;
34
+ _options;
31
35
  actions = {};
32
- constructor(initial, _middleware = null, _parameters = {}) {
36
+ constructor(initial, _middleware = null, _parameters = {}, _options = {}) {
33
37
  this.initial = initial;
34
38
  this._middleware = _middleware;
35
39
  this._parameters = _parameters;
40
+ this._options = _options;
36
41
  }
37
42
  action(name, action) {
38
43
  this.actions[name] = action;
@@ -58,6 +63,8 @@ class HandledServer {
58
63
  cb(err);
59
64
  return;
60
65
  }
66
+ const stream = !!this._options.stream;
67
+ res[context.streamSymbol] = stream;
61
68
  cb(null, res);
62
69
  };
63
70
  }
@@ -1,9 +1,9 @@
1
1
  import { AnyServer, RegisteredServer, Action, GetMiddlewareInput } from './Server';
2
- import { Prettify, ToJson } from './types';
2
+ import { Prettify, ToPrettyJson } from './types';
3
3
  type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
4
4
  type IsAny<T> = IfAny<T, true, never>;
5
5
  type InferInput<R> = R extends Action<infer I, any, any, any, any> ? I : any;
6
- type InferOutput<R> = R extends Action<any, infer O, any, any, any> ? ToJson<O> : never;
6
+ type InferOutput<R> = R extends Action<any, infer O, any, any, any> ? ToPrettyJson<O> : never;
7
7
  type ServerClient<TServer extends AnyServer> = {
8
8
  readonly [Property in keyof TServer['actions']]: (input: Prettify<InferInput<TServer['actions'][Property]> & (IsAny<GetMiddlewareInput<TServer>> extends never ? GetMiddlewareInput<TServer> : {})>) => Promise<InferOutput<TServer['actions'][Property]>>;
9
9
  };
package/dist/types.d.ts CHANGED
@@ -2,9 +2,13 @@ export type Prettify<T> = {
2
2
  [K in keyof T]: T[K];
3
3
  } & {};
4
4
  type JsonPrimitive = string | number | boolean | null;
5
- export type ToJson<T> = T extends Date | RegExp ? string : T extends Function ? never : T extends JsonPrimitive ? T : T extends Array<infer U> ? ToJson<U>[] : T extends object ? {
5
+ type CleanNeverProperties<T> = {
6
+ [K in keyof T as T[K] extends never ? never : K]: T[K];
7
+ };
8
+ type ToJson<T> = T extends Date | RegExp ? string : T extends Function ? never : T extends Promise<any> ? {} : T extends JsonPrimitive ? T : T extends Array<infer U> ? ToJson<U>[] : T extends object ? {
6
9
  [K in keyof T]: ToJson<T[K]>;
7
10
  } : never;
11
+ export type ToPrettyJson<T> = CleanNeverProperties<Prettify<ToJson<T>>>;
8
12
  export interface AcceptLanguage {
9
13
  code: string;
10
14
  script?: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-server",
3
- "version": "1.1.11",
3
+ "version": "2.0.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",