oc-server 0.4.5 → 1.0.0

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,23 +1,41 @@
1
1
  import { DataContext, DataProvider } from './types';
2
+ type Prettify<T> = {
3
+ [K in keyof T]: T[K];
4
+ } & {};
2
5
  export type ServerContext<E = {
3
6
  name: string;
4
- }, P = any> = Omit<DataContext<any, E, P>, 'params' | 'action' | 'setEmptyResponse'>;
5
- export type Action<I, O, E> = (params: I, ctx: ServerContext<E>) => Promise<O> | O;
6
- type AnyAction = Action<any, any, any>;
7
- export declare class Server<E, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any> {
8
- readonly initial: Action<InitialInput, InitialOutput, E>;
7
+ }, P = any, S = any> = Omit<DataContext<any, E, P, S>, 'params' | 'action' | 'setEmptyResponse'>;
8
+ export type Action<I, O, E, P, S> = (params: I, ctx: ServerContext<E, P, S>) => Promise<O> | O;
9
+ type AnyAction = Action<any, any, any, any, any>;
10
+ type IsGeneralStringName<T> = T extends {
11
+ name: infer N;
12
+ } ? N extends string ? string extends N ? true : false : false : false;
13
+ export declare class Server<E = {
14
+ name: string;
15
+ }, P = unknown, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any, MiddlewareInput = any, MiddlewareOutput = any> {
16
+ private _middleware;
17
+ constructor();
18
+ middleware<I, O>(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'>;
19
+ defineEnv<Env>(): P extends Record<string, any> ? Omit<Server<Env, P, A, InitialInput, InitialOutput>, 'defineEnv' | 'definePlugins'> : Omit<Server<Env, P, A, InitialInput, InitialOutput>, 'defineEnv'>;
20
+ definePlugins<Plugins>(): IsGeneralStringName<E> extends true ? Omit<Server<E, Plugins, A, InitialInput, InitialOutput>, 'definePlugins'> : Omit<Server<E, Plugins, A, InitialInput, InitialOutput>, 'definePlugins' | 'defineEnv'>;
21
+ handler<I, O>(action: Action<I, O, E, P, MiddlewareOutput>): HandledServer<E, P, A, I, O, MiddlewareInput, MiddlewareOutput>;
22
+ }
23
+ declare class HandledServer<E, P, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any, MiddlewareInput = any, MiddlewareOutput = any> {
24
+ readonly initial: Action<InitialInput, InitialOutput, E, P, MiddlewareOutput>;
25
+ private readonly _middleware;
9
26
  readonly actions: A;
10
- constructor(initial: Action<InitialInput, InitialOutput, E>);
11
- action<ActionName extends string, I, O>(name: ActionName, action: Action<I, O, E>): Server<E, A & Record<ActionName, Action<I, O, E>>, InitialInput, InitialOutput>;
27
+ constructor(initial: Action<InitialInput, InitialOutput, E, P, MiddlewareOutput>, _middleware?: Action<MiddlewareInput, MiddlewareOutput, E, P, unknown> | null);
28
+ 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>;
12
29
  getData(): DataProvider<any, any, any>;
13
30
  }
14
31
  export interface Register {
15
32
  }
16
- export type AnyServer = Server<any, any>;
33
+ export type AnyServer = HandledServer<any, any>;
17
34
  export type RegisteredServer = Register extends {
18
35
  server: infer TServer extends AnyServer;
19
36
  } ? TServer : AnyServer;
20
- type GetInitialData<TServer extends AnyServer> = TServer extends Server<any, any, any, infer O> ? Exclude<O, undefined | null> : any;
37
+ export type GetMiddlewareInput<TServer extends AnyServer> = TServer extends HandledServer<any, any, any, any, any, infer I> ? I : any;
38
+ type GetInitialData<TServer extends AnyServer> = TServer extends HandledServer<any, any, any, any, infer O> ? Exclude<O, undefined | null> : any;
21
39
  export type InitialData = GetInitialData<RegisteredServer>;
22
40
  export type ComponentSettings = {
23
41
  id: string;
package/dist/Server.js CHANGED
@@ -2,10 +2,30 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getSettings = exports.getInitialData = exports.Server = void 0;
4
4
  class Server {
5
+ _middleware = null;
6
+ constructor() { }
7
+ middleware(action) {
8
+ this._middleware = action;
9
+ return this;
10
+ }
11
+ defineEnv() {
12
+ return this;
13
+ }
14
+ definePlugins() {
15
+ return this;
16
+ }
17
+ handler(action) {
18
+ return new HandledServer(action, this._middleware);
19
+ }
20
+ }
21
+ exports.Server = Server;
22
+ class HandledServer {
5
23
  initial;
24
+ _middleware;
6
25
  actions = {};
7
- constructor(initial) {
26
+ constructor(initial, _middleware = null) {
8
27
  this.initial = initial;
28
+ this._middleware = _middleware;
9
29
  }
10
30
  action(name, action) {
11
31
  this.actions[name] = action;
@@ -15,6 +35,10 @@ class Server {
15
35
  return async ({ action: actionName, params, setEmptyResponse, ...context }, cb) => {
16
36
  let res;
17
37
  try {
38
+ if (this._middleware) {
39
+ const data = await this._middleware(params, context);
40
+ context.state = data;
41
+ }
18
42
  if (actionName && this.actions[actionName]) {
19
43
  const data = params?.data ?? params;
20
44
  res = await this.actions[actionName](data, context);
@@ -31,7 +55,6 @@ class Server {
31
55
  };
32
56
  }
33
57
  }
34
- exports.Server = Server;
35
58
  const getInitialData = () => typeof __$$oc_initialData__ !== 'undefined'
36
59
  ? __$$oc_initialData__
37
60
  : {};
@@ -1,8 +1,13 @@
1
- import { AnyServer, RegisteredServer, Action } from './Server';
2
- type InferInput<R> = R extends Action<infer I, any, any> ? I : any;
3
- type InferOutput<R> = R extends Action<any, infer O, any> ? O : never;
1
+ import { AnyServer, RegisteredServer, Action, GetMiddlewareInput } from './Server';
2
+ type Prettify<T> = {
3
+ [K in keyof T]: T[K];
4
+ } & {};
5
+ type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
6
+ type IsAny<T> = IfAny<T, true, never>;
7
+ type InferInput<R> = R extends Action<infer I, any, any, any, any> ? I : any;
8
+ type InferOutput<R> = R extends Action<any, infer O, any, any, any> ? O : never;
4
9
  type ServerClient<TServer extends AnyServer> = {
5
- readonly [Property in keyof TServer['actions']]: (input: InferInput<TServer['actions'][Property]>) => Promise<InferOutput<TServer['actions'][Property]>>;
10
+ readonly [Property in keyof TServer['actions']]: (input: Prettify<InferInput<TServer['actions'][Property]> & (IsAny<GetMiddlewareInput<TServer>> extends never ? GetMiddlewareInput<TServer> : {})>) => Promise<InferOutput<TServer['actions'][Property]>>;
6
11
  };
7
12
  export declare const serverClient: ServerClient<RegisteredServer>;
8
13
  export type ActionOutput<K extends keyof typeof serverClient> = Awaited<ReturnType<(typeof serverClient)[K]>>;
package/dist/types.d.ts CHANGED
@@ -17,7 +17,7 @@ export interface Template {
17
17
  version: string;
18
18
  externals: External[];
19
19
  }
20
- export interface DataContext<T = any, E = Env, P = any> {
20
+ export type DataContext<T = any, E = Env, P = any, S = unknown> = {
21
21
  action?: string;
22
22
  acceptLanguage: AcceptLanguage[];
23
23
  baseUrl: string;
@@ -30,7 +30,9 @@ export interface DataContext<T = any, E = Env, P = any> {
30
30
  setHeader: (header: string, value: string) => void;
31
31
  staticPath: string;
32
32
  templates: Template[];
33
- }
33
+ } & (S extends Record<string, unknown> ? {
34
+ state: S;
35
+ } : {});
34
36
  type Callback<D, E = Error> = (error: E | null, data?: D) => void;
35
37
  export type DataProvider<Parameters = any, Return = any, Environment = Env> = (context: DataContext<Parameters, Environment>, callback: Callback<Return>) => void;
36
38
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-server",
3
- "version": "0.4.5",
3
+ "version": "1.0.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",