aeria-sdk 0.0.2 → 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')
@@ -1,3 +1,4 @@
1
1
  export { topLevel as Aeria } from './topLevel';
2
2
  export * from '@sonata-api/common';
3
+ export * from './topLevel';
3
4
  export * from '.aeria-sdk';
@@ -18,5 +18,6 @@ exports.Aeria = void 0;
18
18
  var topLevel_1 = require("./topLevel");
19
19
  Object.defineProperty(exports, "Aeria", { enumerable: true, get: function () { return topLevel_1.topLevel; } });
20
20
  __exportStar(require("@sonata-api/common"), exports);
21
+ __exportStar(require("./topLevel"), exports);
21
22
  // @ts-ignore
22
23
  __exportStar(require(".aeria-sdk"), exports);
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.mirror = exports.runtimeJs = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const common_1 = require("@sonata-api/common");
9
+ const promises_1 = require("fs/promises");
10
+ const topLevel_1 = require("./topLevel");
11
+ const mirrorDts = (mirrorObj) => {
12
+ const collections = mirrorObj.descriptions;
13
+ return `import type {
14
+ InferSchema,
15
+ InferResponse,
16
+ SchemaWithId,
17
+ MakeEndpoint,
18
+ CollectionDocument,
19
+ GetPayload,
20
+ GetAllPayload,
21
+ InsertPayload,
22
+ RemovePayload,
23
+ RemoveAllPayload,
24
+ UploadPayload,
25
+ RemoveFilePayload,
26
+ CollectionFunctions
27
+
28
+ } from '@sonata-api/types'
29
+
30
+ declare type MirrorDescriptions = ${JSON.stringify(collections, null, 2)}\n
31
+
32
+ declare type MirrorRouter = ${JSON.stringify(mirrorObj.router, null, 2)}\n
33
+
34
+ declare global {
35
+ type Collections = {
36
+ [K in keyof MirrorDescriptions]: {
37
+ item: SchemaWithId<MirrorDescriptions[K]>
38
+ }
39
+ }
40
+ }
41
+
42
+ declare module 'aeria-sdk' {
43
+ import { TopLevelObject, TLOFunctions } from 'aeria-sdk'
44
+
45
+ type UnionToIntersection<T> = (T extends any ? ((x: T) => 0) : never) extends ((x: infer R) => 0)
46
+ ? R
47
+ : never
48
+
49
+ type Endpoints = {
50
+ [Route in keyof MirrorRouter]: MirrorRouter[Route] extends infer RouteContract
51
+ ? RouteContract extends [infer RoutePayload, infer RouteResponse]
52
+ ? RoutePayload extends null
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>
58
+ : never
59
+ } extends infer Endpoints
60
+ ? UnionToIntersection<Endpoints[keyof Endpoints]>
61
+ : never
62
+
63
+ type StrongelyTypedTLO = TopLevelObject & Endpoints & {
64
+ [K in keyof MirrorDescriptions]: SchemaWithId<MirrorDescriptions[K]> extends infer Document
65
+ ? CollectionFunctions<Document> & Omit<TLOFunctions, keyof Functions>
66
+ : never
67
+ }
68
+
69
+ export const url: string
70
+ export const aeria: StrongelyTypedTLO
71
+ }\n
72
+ `;
73
+ };
74
+ const runtimeJs = (config) => `exports.url = '${(0, topLevel_1.apiUrl)(config)}'
75
+ exports.aeria = require('aeria-sdk').Aeria(${JSON.stringify(config)})\n
76
+ `;
77
+ exports.runtimeJs = runtimeJs;
78
+ const mirror = async (config) => {
79
+ const api = (0, topLevel_1.topLevel)(config);
80
+ const runtimeBase = path_1.default.join(path_1.default.dirname(require.resolve('aeria-sdk')), '..', '..', '..', '.aeria-sdk');
81
+ const mirror = (0, common_1.deserialize)(await api.describe({
82
+ router: true
83
+ }));
84
+ await (0, promises_1.mkdir)(runtimeBase, { recursive: true });
85
+ await (0, promises_1.writeFile)(path_1.default.join(process.cwd(), 'aeria-sdk.d.ts'), mirrorDts(mirror));
86
+ await (0, promises_1.writeFile)(path_1.default.join(runtimeBase, 'index.js'), (0, exports.runtimeJs)(config));
87
+ };
88
+ exports.mirror = mirror;
@@ -5,9 +5,12 @@ type UserFunctions = {
5
5
  authenticate: (payload: AuthenticationPayload) => Promise<any>;
6
6
  };
7
7
  };
8
- type TLOFunctions = {
8
+ export type TLOFunctions = {
9
9
  [P in string]: ((payload?: any) => Promise<any>) & TLOFunctions;
10
10
  };
11
- type TopLevelObject = TLOFunctions & UserFunctions;
11
+ export type TopLevelObject = UserFunctions & {
12
+ describe: (...args: any) => Promise<any>;
13
+ };
14
+ export declare const apiUrl: (config: InstanceConfig) => string;
12
15
  export declare const topLevel: (config: InstanceConfig) => TopLevelObject;
13
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.2",
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/mirror.js DELETED
@@ -1,71 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.mirror = exports.runtimeJs = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const common_1 = require("@sonata-api/common");
9
- const promises_1 = require("fs/promises");
10
- const topLevel_1 = require("./topLevel");
11
- const mirrorDts = (mirrorObj) => `import type {
12
- Schema,
13
- CollectionDocument,
14
- GetPayload,
15
- GetAllPayload,
16
- InsertPayload,
17
- RemovePayload,
18
- RemoveAllPayload,
19
- UploadPayload,
20
- RemoveFilePayload
21
-
22
- } from '@sonata-api/types'
23
-
24
- declare type MirrorDescriptions = ${JSON.stringify(mirrorObj.descriptions, null, 2)}\n
25
-
26
- declare type CollectionFunctions<
27
- TCollectionName extends keyof MirrorDescriptions
28
- > = Schema<MirrorDescriptions[TCollectionName]> extends infer Document
29
- ? Document extends CollectionDocument<any>
30
- ? {
31
- get: (payload: GetPayload<Document>) => Promise<Document>
32
- getAll: (payload?: GetAllPayload<Document>) => Promise<Document[]>
33
- insert: (payload: InsertPayload<Document>) => Promise<Document>
34
- upload: (payload: UploadPayload<Document>) => Promise<any>
35
- remove: (payload: RemovePayload<Document>) => Promise<Document>
36
- removeAll: (payload: RemoveAllPayload<Document>) => Promise<any>
37
- removeFile: (payload: UploadPayload<Document>) => Promise<any>
38
- }
39
- : never
40
- : never
41
-
42
- declare module 'aeria-sdk' {
43
- import {
44
- InstanceConfig,
45
- TopLevelObject,
46
- TLOFunctions
47
- } from 'aeria-sdk'
48
-
49
- type StrongelyTypedTLO = Omit<TopLevelObject, keyof MirrorDescriptions> & {
50
- [K in keyof MirrorDescriptions]: CollectionFunctions<K> extends infer Functions
51
- ? Functions & Omit<TLOFunctions, keyof Functions>
52
- : never
53
- }
54
-
55
- export const url: string
56
- export const aeria: StrongelyTypedTLO
57
- }
58
- `;
59
- const runtimeJs = (config) => `exports.url = '${config.apiUrl}'
60
- exports.aeria = require('aeria-sdk').Aeria(${JSON.stringify(config)})
61
- `;
62
- exports.runtimeJs = runtimeJs;
63
- const mirror = async (config) => {
64
- const api = (0, topLevel_1.topLevel)(config);
65
- const runtimeBase = path_1.default.join(process.cwd(), 'node_modules', '.aeria-sdk');
66
- const mirror = (0, common_1.deserialize)(await api.describe());
67
- await (0, promises_1.mkdir)(runtimeBase, { recursive: true });
68
- await (0, promises_1.writeFile)(path_1.default.join(process.cwd(), 'aeria-sdk.d.ts'), mirrorDts(mirror));
69
- await (0, promises_1.writeFile)(path_1.default.join(runtimeBase, 'index.js'), (0, exports.runtimeJs)(config));
70
- };
71
- exports.mirror = mirror;
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