aeria-sdk 0.0.3 → 0.0.4

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/bin/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- require('../dist/cli')
3
+ require('../dist/cjs/cli')
@@ -12,6 +12,7 @@ const mirrorDts = (mirrorObj) => {
12
12
  const collections = mirrorObj.descriptions;
13
13
  return `import type {
14
14
  InferSchema,
15
+ InferResponse,
15
16
  SchemaWithId,
16
17
  MakeEndpoint,
17
18
  CollectionDocument,
@@ -49,9 +50,11 @@ declare module 'aeria-sdk' {
49
50
  [Route in keyof MirrorRouter]: MirrorRouter[Route] extends infer RouteContract
50
51
  ? RouteContract extends [infer RoutePayload, infer RouteResponse]
51
52
  ? RoutePayload extends null
52
- ? MakeEndpoint<Route, InferSchema<RouteResponse>, undefined>
53
- : MakeEndpoint<Route, InferSchema<RouteResponse>, InferSchema<RoutePayload>>
54
- : MakeEndpoint<Route>
53
+ ? MakeEndpoint<Route, InferResponse<RouteResponse>, undefined>
54
+ : MakeEndpoint<Route, InferResponse<RouteResponse>, InferSchema<RoutePayload>>
55
+ : RouteContract extends Record<string, any>
56
+ ? MakeEndpoint<Route, any, InferSchema<RouteContract>>
57
+ : MakeEndpoint<Route>
55
58
  : never
56
59
  } extends infer Endpoints
57
60
  ? UnionToIntersection<Endpoints[keyof Endpoints]>
@@ -68,13 +71,13 @@ declare module 'aeria-sdk' {
68
71
  }\n
69
72
  `;
70
73
  };
71
- const runtimeJs = (config) => `exports.url = '${config.apiUrl}'
74
+ const runtimeJs = (config) => `exports.url = '${(0, topLevel_1.apiUrl)(config)}'
72
75
  exports.aeria = require('aeria-sdk').Aeria(${JSON.stringify(config)})\n
73
76
  `;
74
77
  exports.runtimeJs = runtimeJs;
75
78
  const mirror = async (config) => {
76
79
  const api = (0, topLevel_1.topLevel)(config);
77
- const runtimeBase = path_1.default.join(process.cwd(), 'node_modules', '.aeria-sdk');
80
+ const runtimeBase = path_1.default.join(path_1.default.dirname(require.resolve('aeria-sdk')), '..', '..', '..', '.aeria-sdk');
78
81
  const mirror = (0, common_1.deserialize)(await api.describe({
79
82
  router: true
80
83
  }));
@@ -11,5 +11,6 @@ export type TLOFunctions = {
11
11
  export type TopLevelObject = UserFunctions & {
12
12
  describe: (...args: any) => Promise<any>;
13
13
  };
14
+ export declare const apiUrl: (config: InstanceConfig) => string;
14
15
  export declare const topLevel: (config: InstanceConfig) => TopLevelObject;
15
16
  export {};
@@ -1,8 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.topLevel = void 0;
3
+ exports.topLevel = exports.apiUrl = void 0;
4
4
  const auth_1 = require("./auth");
5
5
  const http_1 = require("./http");
6
+ const apiUrl = (config) => {
7
+ if (typeof config.apiUrl === 'string') {
8
+ return config.apiUrl;
9
+ }
10
+ return process.env.NODE_ENV === 'production'
11
+ ? config.apiUrl.production
12
+ : config.apiUrl.development;
13
+ };
14
+ exports.apiUrl = apiUrl;
6
15
  const topLevel = (config) => {
7
16
  const proxify = (target, parent) => new Proxy(target, {
8
17
  get: (_, key) => {
@@ -14,8 +23,8 @@ const topLevel = (config) => {
14
23
  }
15
24
  const fn = async (payload) => {
16
25
  const response = payload
17
- ? await (0, http_1.request)(`${config.apiUrl}/${endpoint}`, payload)
18
- : await (0, http_1.request)(`${config.apiUrl}/${endpoint}`);
26
+ ? await (0, http_1.request)(`${(0, exports.apiUrl)(config)}/${endpoint}`, payload)
27
+ : await (0, http_1.request)(`${(0, exports.apiUrl)(config)}/${endpoint}`);
19
28
  return response.data;
20
29
  };
21
30
  return proxify(fn, endpoint);
@@ -24,12 +33,3 @@ const topLevel = (config) => {
24
33
  return proxify({});
25
34
  };
26
35
  exports.topLevel = topLevel;
27
- (async () => {
28
- const aeria = (0, exports.topLevel)({
29
- apiUrl: 'https://pedidos.capsulbrasil.com.br/api'
30
- });
31
- await aeria.user.authenticate({
32
- email: 'root',
33
- password: '12569874'
34
- });
35
- })();
@@ -0,0 +1,6 @@
1
+ export type InstanceConfig = {
2
+ apiUrl: string | {
3
+ production: string;
4
+ development: string;
5
+ };
6
+ };
@@ -0,0 +1,14 @@
1
+ import type { InstanceConfig } from './types';
2
+ export type AuthenticationResult = {
3
+ user: any;
4
+ token: {
5
+ type: 'bearer';
6
+ content: string;
7
+ };
8
+ };
9
+ export type AuthenticationPayload = {
10
+ email: string;
11
+ password: string;
12
+ };
13
+ export declare const authMemo: AuthenticationResult;
14
+ export declare const authenticate: (config: InstanceConfig) => (payload: AuthenticationPayload) => Promise<unknown>;
@@ -0,0 +1,14 @@
1
+ import { isLeft, unwrapEither } from '@sonata-api/common';
2
+ import { request } from './http';
3
+ export const authMemo = {};
4
+ export const authenticate = (config) => async (payload) => {
5
+ const response = await request(`${config.apiUrl}/user/authenticate`, payload);
6
+ const resultEither = response.data;
7
+ if (isLeft(resultEither)) {
8
+ //
9
+ return;
10
+ }
11
+ const result = unwrapEither(resultEither);
12
+ Object.assign(authMemo, result);
13
+ return result;
14
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import path from 'path';
2
+ import { mirror } from './mirror';
3
+ const main = async () => {
4
+ const { aeriaSdk } = require(path.join(process.cwd(), 'package.json'));
5
+ if (typeof aeriaSdk !== 'object' || !aeriaSdk) {
6
+ console.log('aeriaSdk is absent in package.json');
7
+ return;
8
+ }
9
+ mirror(aeriaSdk);
10
+ };
11
+ main();
@@ -0,0 +1,2 @@
1
+ import { type RequestConfig } from '@sonata-api/common';
2
+ export declare const request: <Return = any>(url: string, payload?: any, _config?: RequestConfig) => Promise<Return>;
@@ -0,0 +1,19 @@
1
+ import { request as originalRequest, defaultRequestTransformer } from '@sonata-api/common';
2
+ import { authMemo } from './auth';
3
+ export const request = (url, payload, _config) => {
4
+ const config = Object.assign({}, _config);
5
+ config.requestTransformer ??= async (url, payload, _params) => {
6
+ const params = Object.assign({}, _params);
7
+ if (authMemo.token) {
8
+ params.headers ??= {};
9
+ switch (authMemo.token.type) {
10
+ case 'bearer': {
11
+ params.headers.authorization = `Bearer ${authMemo.token.content}`;
12
+ break;
13
+ }
14
+ }
15
+ }
16
+ return defaultRequestTransformer(url, payload, params);
17
+ };
18
+ return originalRequest(url, payload, config);
19
+ };
@@ -0,0 +1,4 @@
1
+ export { topLevel as Aeria } from './topLevel';
2
+ export * from '@sonata-api/common';
3
+ export * from './topLevel';
4
+ export * from '.aeria-sdk';
@@ -0,0 +1,5 @@
1
+ export { topLevel as Aeria } from './topLevel';
2
+ export * from '@sonata-api/common';
3
+ export * from './topLevel';
4
+ // @ts-ignore
5
+ export * from '.aeria-sdk';
@@ -0,0 +1,3 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const runtimeJs: (config: InstanceConfig) => string;
3
+ export declare const mirror: (config: InstanceConfig) => Promise<void>;
@@ -0,0 +1,80 @@
1
+ import path from 'path';
2
+ import { deserialize } from '@sonata-api/common';
3
+ import { writeFile, mkdir } from 'fs/promises';
4
+ import { topLevel, apiUrl } from './topLevel';
5
+ const mirrorDts = (mirrorObj) => {
6
+ const collections = mirrorObj.descriptions;
7
+ return `import type {
8
+ InferSchema,
9
+ InferResponse,
10
+ SchemaWithId,
11
+ MakeEndpoint,
12
+ CollectionDocument,
13
+ GetPayload,
14
+ GetAllPayload,
15
+ InsertPayload,
16
+ RemovePayload,
17
+ RemoveAllPayload,
18
+ UploadPayload,
19
+ RemoveFilePayload,
20
+ CollectionFunctions
21
+
22
+ } from '@sonata-api/types'
23
+
24
+ declare type MirrorDescriptions = ${JSON.stringify(collections, null, 2)}\n
25
+
26
+ declare type MirrorRouter = ${JSON.stringify(mirrorObj.router, null, 2)}\n
27
+
28
+ declare global {
29
+ type Collections = {
30
+ [K in keyof MirrorDescriptions]: {
31
+ item: SchemaWithId<MirrorDescriptions[K]>
32
+ }
33
+ }
34
+ }
35
+
36
+ declare module 'aeria-sdk' {
37
+ import { TopLevelObject, TLOFunctions } from 'aeria-sdk'
38
+
39
+ type UnionToIntersection<T> = (T extends any ? ((x: T) => 0) : never) extends ((x: infer R) => 0)
40
+ ? R
41
+ : never
42
+
43
+ type Endpoints = {
44
+ [Route in keyof MirrorRouter]: MirrorRouter[Route] extends infer RouteContract
45
+ ? RouteContract extends [infer RoutePayload, infer RouteResponse]
46
+ ? RoutePayload extends null
47
+ ? MakeEndpoint<Route, InferResponse<RouteResponse>, undefined>
48
+ : MakeEndpoint<Route, InferResponse<RouteResponse>, InferSchema<RoutePayload>>
49
+ : RouteContract extends Record<string, any>
50
+ ? MakeEndpoint<Route, any, InferSchema<RouteContract>>
51
+ : MakeEndpoint<Route>
52
+ : never
53
+ } extends infer Endpoints
54
+ ? UnionToIntersection<Endpoints[keyof Endpoints]>
55
+ : never
56
+
57
+ type StrongelyTypedTLO = TopLevelObject & Endpoints & {
58
+ [K in keyof MirrorDescriptions]: SchemaWithId<MirrorDescriptions[K]> extends infer Document
59
+ ? CollectionFunctions<Document> & Omit<TLOFunctions, keyof Functions>
60
+ : never
61
+ }
62
+
63
+ export const url: string
64
+ export const aeria: StrongelyTypedTLO
65
+ }\n
66
+ `;
67
+ };
68
+ export const runtimeJs = (config) => `exports.url = '${apiUrl(config)}'
69
+ exports.aeria = require('aeria-sdk').Aeria(${JSON.stringify(config)})\n
70
+ `;
71
+ export const mirror = async (config) => {
72
+ const api = topLevel(config);
73
+ const runtimeBase = path.join(path.dirname(require.resolve('aeria-sdk')), '..', '..', '..', '.aeria-sdk');
74
+ const mirror = deserialize(await api.describe({
75
+ router: true
76
+ }));
77
+ await mkdir(runtimeBase, { recursive: true });
78
+ await writeFile(path.join(process.cwd(), 'aeria-sdk.d.ts'), mirrorDts(mirror));
79
+ await writeFile(path.join(runtimeBase, 'index.js'), runtimeJs(config));
80
+ };
@@ -0,0 +1,16 @@
1
+ import type { InstanceConfig } from './types';
2
+ import { type AuthenticationPayload } from './auth';
3
+ type UserFunctions = {
4
+ user: TLOFunctions & {
5
+ authenticate: (payload: AuthenticationPayload) => Promise<any>;
6
+ };
7
+ };
8
+ export type TLOFunctions = {
9
+ [P in string]: ((payload?: any) => Promise<any>) & TLOFunctions;
10
+ };
11
+ export type TopLevelObject = UserFunctions & {
12
+ describe: (...args: any) => Promise<any>;
13
+ };
14
+ export declare const apiUrl: (config: InstanceConfig) => string;
15
+ export declare const topLevel: (config: InstanceConfig) => TopLevelObject;
16
+ export {};
@@ -0,0 +1,30 @@
1
+ import { authenticate } from './auth';
2
+ import { request } from './http';
3
+ export const apiUrl = (config) => {
4
+ if (typeof config.apiUrl === 'string') {
5
+ return config.apiUrl;
6
+ }
7
+ return process.env.NODE_ENV === 'production'
8
+ ? config.apiUrl.production
9
+ : config.apiUrl.development;
10
+ };
11
+ export const topLevel = (config) => {
12
+ const proxify = (target, parent) => new Proxy(target, {
13
+ get: (_, key) => {
14
+ const endpoint = parent
15
+ ? `${parent}/${key}`
16
+ : `${key}`;
17
+ switch (endpoint) {
18
+ case 'user/authenticate': return authenticate(config);
19
+ }
20
+ const fn = async (payload) => {
21
+ const response = payload
22
+ ? await request(`${apiUrl(config)}/${endpoint}`, payload)
23
+ : await request(`${apiUrl(config)}/${endpoint}`);
24
+ return response.data;
25
+ };
26
+ return proxify(fn, endpoint);
27
+ }
28
+ });
29
+ return proxify({});
30
+ };
@@ -0,0 +1,6 @@
1
+ export type InstanceConfig = {
2
+ apiUrl: string | {
3
+ production: string;
4
+ development: string;
5
+ };
6
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "aeria-sdk",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
5
+ "main": "dist/cjs/index.js",
6
+ "types": "dist/cjs/index.d.ts",
7
7
  "keywords": [],
8
8
  "author": "",
9
9
  "license": "ISC",
@@ -13,6 +13,16 @@
13
13
  "bin": {
14
14
  "aeria-sdk": "bin/index.js"
15
15
  },
16
+ "files": [
17
+ "dist",
18
+ "bin"
19
+ ],
20
+ "exports": {
21
+ ".": {
22
+ "import": "./dist/esm/index.js",
23
+ "require": "./dist/cjs/index.js"
24
+ }
25
+ },
16
26
  "peerDependencies": {
17
27
  "@sonata-api/common": "*",
18
28
  "@sonata-api/types": "*"
@@ -20,13 +30,9 @@
20
30
  "devDependencies": {
21
31
  "@types/node": "^20.10.4"
22
32
  },
23
- "files": [
24
- "dist",
25
- "bin"
26
- ],
27
33
  "scripts": {
28
- "test": "echo \"Error: no test specified\" && exit 1",
29
- "build": "tsc",
34
+ "test": "echo skipping",
35
+ "build": "tsc -p tsconfig.json; tsc -p tsconfig.esm.json",
30
36
  "watch": "tsc --watch"
31
37
  }
32
38
  }
package/dist/types.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export type InstanceConfig = {
2
- apiUrl: string;
3
- };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes