oc-server 0.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/README.md +3 -0
- package/dist/Server.d.ts +18 -0
- package/dist/Server.js +29 -0
- package/dist/index.d.ts +0 -0
- package/dist/index.js +2 -0
- package/dist/serverClient.d.ts +8 -0
- package/dist/serverClient.js +15 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.js +2 -0
- package/package.json +20 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +13 -0
package/README.md
ADDED
package/dist/Server.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Context } from './types';
|
|
2
|
+
type ContextWithoutParams<E> = Omit<Context<any, E>, 'params'>;
|
|
3
|
+
export type Action<I, O, E> = (params: I, ctx: ContextWithoutParams<E>) => Promise<O> | O;
|
|
4
|
+
type AnyAction = Action<any, any, any>;
|
|
5
|
+
export declare class Server<E, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any> {
|
|
6
|
+
private initial;
|
|
7
|
+
actions: A;
|
|
8
|
+
constructor(initial: Action<InitialInput, InitialOutput, E>);
|
|
9
|
+
action<ActionName extends string, I, O>(name: ActionName, action: Action<I, O, E>): Server<E, A & Record<ActionName, Action<I, O, E>>, InitialInput, InitialOutput>;
|
|
10
|
+
getData(): (context: any, cb: any) => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export interface Register {
|
|
13
|
+
}
|
|
14
|
+
export type AnyServer = Server<any, any>;
|
|
15
|
+
export type RegisteredServer = Register extends {
|
|
16
|
+
server: infer TServer extends AnyServer;
|
|
17
|
+
} ? TServer : AnyServer;
|
|
18
|
+
export {};
|
package/dist/Server.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Server = void 0;
|
|
4
|
+
const actionKey = '__oc--action__';
|
|
5
|
+
class Server {
|
|
6
|
+
initial;
|
|
7
|
+
actions = {};
|
|
8
|
+
constructor(initial) {
|
|
9
|
+
this.initial = initial;
|
|
10
|
+
}
|
|
11
|
+
action(name, action) {
|
|
12
|
+
this.actions[name] = action;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
getData() {
|
|
16
|
+
return async (context, cb) => {
|
|
17
|
+
const { [actionKey]: actionName, ...params } = context.params;
|
|
18
|
+
const action = this.actions[actionName] ?? this.initial;
|
|
19
|
+
try {
|
|
20
|
+
const res = await action(params, context);
|
|
21
|
+
cb(null, res);
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
cb(err);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.Server = Server;
|
package/dist/index.d.ts
ADDED
|
File without changes
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
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;
|
|
4
|
+
type ServerClient<TServer extends AnyServer> = {
|
|
5
|
+
readonly [Property in keyof TServer['actions']]: (input: InferInput<TServer['actions'][Property]>) => Promise<InferOutput<TServer['actions'][Property]>>;
|
|
6
|
+
};
|
|
7
|
+
export declare const serverClient: ServerClient<RegisteredServer>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serverClient = void 0;
|
|
4
|
+
const actionKey = '__oc--action__';
|
|
5
|
+
exports.serverClient = new Proxy({}, {
|
|
6
|
+
get(_target, prop) {
|
|
7
|
+
return (data) => {
|
|
8
|
+
const { getData } = useDataCompiled();
|
|
9
|
+
return getData({
|
|
10
|
+
[actionKey]: prop,
|
|
11
|
+
data,
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
});
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface AcceptLanguage {
|
|
2
|
+
code: string;
|
|
3
|
+
script?: any;
|
|
4
|
+
region: string;
|
|
5
|
+
quality: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Env {
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
export interface Plugins {
|
|
11
|
+
}
|
|
12
|
+
export interface External {
|
|
13
|
+
global: string;
|
|
14
|
+
url: string;
|
|
15
|
+
name: string;
|
|
16
|
+
}
|
|
17
|
+
export interface Template {
|
|
18
|
+
type: string;
|
|
19
|
+
version: string;
|
|
20
|
+
externals: External[];
|
|
21
|
+
}
|
|
22
|
+
export interface Context<T = any, E = Env> {
|
|
23
|
+
acceptLanguage: AcceptLanguage[];
|
|
24
|
+
baseUrl: string;
|
|
25
|
+
env: E;
|
|
26
|
+
params: T;
|
|
27
|
+
plugins: Plugins;
|
|
28
|
+
requestHeaders: Record<string, string>;
|
|
29
|
+
requestIp: string;
|
|
30
|
+
setEmptyResponse: () => void;
|
|
31
|
+
setHeader: (header: string, value: string) => void;
|
|
32
|
+
staticPath: string;
|
|
33
|
+
templates: Template[];
|
|
34
|
+
}
|
|
35
|
+
type Callback<D, E = Error> = (error: E | null, data?: D) => void;
|
|
36
|
+
export type DataProvider<Parameters, Return, Environment = Env> = (context: Context<Parameters, Environment>, callback: Callback<Return>) => void;
|
|
37
|
+
export {};
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "oc-server",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepare": "npm run build"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"oc",
|
|
13
|
+
"opencomponents"
|
|
14
|
+
],
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Ricardo Devis Agullo",
|
|
17
|
+
"email": "mkredz@gmail.com"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT"
|
|
20
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
throw new Error('Not implemented yet!');
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"forceConsistentCasingInFileNames": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"skipLibCheck": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["./src"]
|
|
13
|
+
}
|