aeria-sdk 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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright 2023 João Santos (joaosan177@gmail.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the “Software”), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # `@waltz-ui/sdk`
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ yarn add @waltz-ui/sdk
7
+ # or
8
+ $ npm install @waltz-ui/sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ TODO: provide basic documentation.
package/bin/index.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../dist/cli')
package/dist/auth.d.ts ADDED
@@ -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>;
package/dist/auth.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.authenticate = exports.authMemo = void 0;
4
+ const common_1 = require("@sonata-api/common");
5
+ const http_1 = require("./http");
6
+ exports.authMemo = {};
7
+ const authenticate = (config) => async (payload) => {
8
+ const response = await (0, http_1.request)(`${config.apiUrl}/user/authenticate`, payload);
9
+ const resultEither = response.data;
10
+ if ((0, common_1.isLeft)(resultEither)) {
11
+ //
12
+ return;
13
+ }
14
+ const result = (0, common_1.unwrapEither)(resultEither);
15
+ Object.assign(exports.authMemo, result);
16
+ return result;
17
+ };
18
+ exports.authenticate = authenticate;
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,16 @@
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
+ const path_1 = __importDefault(require("path"));
7
+ const mirror_1 = require("./mirror");
8
+ const main = async () => {
9
+ const { aeriaSdk } = require(path_1.default.join(process.cwd(), 'package.json'));
10
+ if (typeof aeriaSdk !== 'object' || !aeriaSdk) {
11
+ console.log('aeriaSdk is absent in package.json');
12
+ return;
13
+ }
14
+ (0, mirror_1.mirror)(aeriaSdk);
15
+ };
16
+ main();
package/dist/http.d.ts ADDED
@@ -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>;
package/dist/http.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.request = void 0;
4
+ const common_1 = require("@sonata-api/common");
5
+ const auth_1 = require("./auth");
6
+ const request = (url, payload, _config) => {
7
+ const config = Object.assign({}, _config);
8
+ config.requestTransformer ??= async (url, payload, _params) => {
9
+ const params = Object.assign({}, _params);
10
+ if (auth_1.authMemo.token) {
11
+ params.headers ??= {};
12
+ switch (auth_1.authMemo.token.type) {
13
+ case 'bearer': {
14
+ params.headers.authorization = `Bearer ${auth_1.authMemo.token.content}`;
15
+ break;
16
+ }
17
+ }
18
+ }
19
+ return (0, common_1.defaultRequestTransformer)(url, payload, params);
20
+ };
21
+ return (0, common_1.request)(url, payload, config);
22
+ };
23
+ exports.request = request;
@@ -0,0 +1,3 @@
1
+ export { topLevel as Aeria } from './topLevel';
2
+ export * from '@sonata-api/common';
3
+ export * from '.aeria-sdk';
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
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
+ exports.Aeria = void 0;
18
+ var topLevel_1 = require("./topLevel");
19
+ Object.defineProperty(exports, "Aeria", { enumerable: true, get: function () { return topLevel_1.topLevel; } });
20
+ __exportStar(require("@sonata-api/common"), exports);
21
+ // @ts-ignore
22
+ __exportStar(require(".aeria-sdk"), exports);
@@ -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>;
package/dist/mirror.js ADDED
@@ -0,0 +1,71 @@
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;
@@ -0,0 +1,13 @@
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
+ type TLOFunctions = {
9
+ [P in string]: ((payload?: any) => Promise<any>) & TLOFunctions;
10
+ };
11
+ type TopLevelObject = TLOFunctions & UserFunctions;
12
+ export declare const topLevel: (config: InstanceConfig) => TopLevelObject;
13
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.topLevel = void 0;
4
+ const auth_1 = require("./auth");
5
+ const http_1 = require("./http");
6
+ const topLevel = (config) => {
7
+ const proxify = (target, parent) => new Proxy(target, {
8
+ get: (_, key) => {
9
+ const endpoint = parent
10
+ ? `${parent}/${key}`
11
+ : `${key}`;
12
+ switch (endpoint) {
13
+ case 'user/authenticate': return (0, auth_1.authenticate)(config);
14
+ }
15
+ const fn = async (payload) => {
16
+ const response = payload
17
+ ? await (0, http_1.request)(`${config.apiUrl}/${endpoint}`, payload)
18
+ : await (0, http_1.request)(`${config.apiUrl}/${endpoint}`);
19
+ return response.data;
20
+ };
21
+ return proxify(fn, endpoint);
22
+ }
23
+ });
24
+ return proxify({});
25
+ };
26
+ 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,3 @@
1
+ export type InstanceConfig = {
2
+ apiUrl: string;
3
+ };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "aeria-sdk",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "keywords": [],
8
+ "author": "",
9
+ "license": "ISC",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "bin": {
14
+ "aeria-sdk": "bin/index.js"
15
+ },
16
+ "peerDependencies": {
17
+ "@sonata-api/common": "*",
18
+ "@sonata-api/types": "*"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^20.10.4"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "bin"
26
+ ],
27
+ "scripts": {
28
+ "test": "echo \"Error: no test specified\" && exit 1",
29
+ "build": "tsc",
30
+ "watch": "tsc --watch"
31
+ }
32
+ }