oc-server 0.0.1 → 0.0.2-beta.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,13 +1,13 @@
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;
1
+ import { Context, DataProvider } from './types';
2
+ type ContextWithoutParamsActions<E> = Omit<Context<any, E>, 'params' | 'action'>;
3
+ export type Action<I, O, E> = (params: I, ctx: ContextWithoutParamsActions<E>) => Promise<O> | O;
4
4
  type AnyAction = Action<any, any, any>;
5
5
  export declare class Server<E, A extends Record<string, AnyAction> = {}, InitialInput = any, InitialOutput = any> {
6
6
  private initial;
7
7
  actions: A;
8
8
  constructor(initial: Action<InitialInput, InitialOutput, E>);
9
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>;
10
+ getData(): DataProvider<any, any, any>;
11
11
  }
12
12
  export interface Register {
13
13
  }
package/dist/Server.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Server = void 0;
4
- const actionKey = '__oc--action__';
5
4
  class Server {
6
5
  initial;
7
6
  actions = {};
@@ -13,11 +12,15 @@ class Server {
13
12
  return this;
14
13
  }
15
14
  getData() {
16
- return async (context, cb) => {
17
- const { [actionKey]: actionName, ...params } = context.params;
18
- const action = this.actions[actionName] ?? this.initial;
15
+ return async ({ action: actionName, params, ...context }, cb) => {
16
+ let res;
19
17
  try {
20
- const res = await action(params, context);
18
+ if (actionName && this.actions[actionName]) {
19
+ res = await this.actions[actionName](params, context);
20
+ }
21
+ else {
22
+ res = await this.initial(params, context);
23
+ }
21
24
  cb(null, res);
22
25
  }
23
26
  catch (err) {
package/dist/index.d.ts CHANGED
@@ -0,0 +1,2 @@
1
+ export * from './Server';
2
+ export * from './serverClient';
package/dist/index.js CHANGED
@@ -1,2 +1,18 @@
1
1
  "use strict";
2
- throw new Error('Not implemented yet!');
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Server"), exports);
18
+ __exportStar(require("./serverClient"), exports);
@@ -1,14 +1,46 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.serverClient = void 0;
4
- const actionKey = '__oc--action__';
4
+ function getAction(options) {
5
+ return new Promise((resolve, reject) => {
6
+ window.oc.getData({
7
+ action: options.action,
8
+ name: options.componentName,
9
+ version: options.componentVersion,
10
+ baseUrl: options.baseUrl,
11
+ parameters: options.parameters,
12
+ }, (err, data) => {
13
+ if (err) {
14
+ return reject(err);
15
+ }
16
+ const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = data.component.props;
17
+ resolve(rest);
18
+ });
19
+ });
20
+ }
21
+ console.log('HELLO');
5
22
  exports.serverClient = new Proxy({}, {
6
23
  get(_target, prop) {
7
24
  return (data) => {
8
- const { getData } = useDataCompiled();
9
- return getData({
10
- [actionKey]: prop,
11
- data,
25
+ const componentName = '';
26
+ const componentVersion = '';
27
+ let url = document
28
+ .querySelector(`oc-component[data-name="${componentName}"][data-version="${componentVersion}"]`)
29
+ ?.getAttribute('href');
30
+ if (!url?.startsWith('http')) {
31
+ url = `${window.location.protocol}${url}`;
32
+ }
33
+ const base = new URL(url);
34
+ const baseUrl = base.origin +
35
+ base.pathname.replace(new RegExp(`/${componentName}/[\\dx]\.[\\dx]\.[\\dx].*`), '');
36
+ return getAction({
37
+ action: prop,
38
+ baseUrl,
39
+ componentName,
40
+ componentVersion,
41
+ parameters: {
42
+ data,
43
+ },
12
44
  });
13
45
  };
14
46
  },
package/dist/types.d.ts CHANGED
@@ -20,6 +20,7 @@ export interface Template {
20
20
  externals: External[];
21
21
  }
22
22
  export interface Context<T = any, E = Env> {
23
+ action?: string;
23
24
  acceptLanguage: AcceptLanguage[];
24
25
  baseUrl: string;
25
26
  env: E;
@@ -33,5 +34,5 @@ export interface Context<T = any, E = Env> {
33
34
  templates: Template[];
34
35
  }
35
36
  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 type DataProvider<Parameters = any, Return = any, Environment = Env> = (context: Context<Parameters, Environment>, callback: Callback<Return>) => void;
37
38
  export {};
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "oc-server",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-beta.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
- "prepare": "npm run build"
9
+ "prepare": "npm run build",
10
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
10
11
  },
11
12
  "keywords": [
12
13
  "oc",
@@ -16,5 +17,8 @@
16
17
  "name": "Ricardo Devis Agullo",
17
18
  "email": "mkredz@gmail.com"
18
19
  },
20
+ "files": [
21
+ "dist/**"
22
+ ],
19
23
  "license": "MIT"
20
24
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # OC Server
2
-
3
- WIP - This is a new set of utils for interacting with the server side of OCs.
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- throw new Error('Not implemented yet!');
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
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
- }