aeria-sdk 0.0.3 → 0.0.5

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.
Files changed (47) hide show
  1. package/bin/index.js +1 -1
  2. package/dist/{auth.d.ts → cjs/auth.d.ts} +2 -1
  3. package/dist/cjs/auth.js +22 -0
  4. package/dist/cjs/http.d.ts +3 -0
  5. package/dist/cjs/http.js +27 -0
  6. package/dist/{index.d.ts → cjs/index.d.ts} +2 -1
  7. package/dist/{index.js → cjs/index.js} +2 -2
  8. package/dist/cjs/mirror.d.ts +4 -0
  9. package/dist/{mirror.js → cjs/mirror.js} +26 -10
  10. package/dist/cjs/runtime.d.ts +9 -0
  11. package/dist/cjs/runtime.js +7 -0
  12. package/dist/cjs/storage.d.ts +8 -0
  13. package/dist/cjs/storage.js +46 -0
  14. package/dist/{topLevel.d.ts → cjs/topLevel.d.ts} +2 -0
  15. package/dist/{topLevel.js → cjs/topLevel.js} +7 -11
  16. package/dist/cjs/types.d.ts +10 -0
  17. package/dist/cjs/utils.d.ts +2 -0
  18. package/dist/cjs/utils.js +12 -0
  19. package/dist/esm/auth.d.ts +15 -0
  20. package/dist/esm/auth.js +17 -0
  21. package/dist/esm/cli.d.ts +1 -0
  22. package/dist/esm/cli.js +11 -0
  23. package/dist/esm/http.d.ts +3 -0
  24. package/dist/esm/http.js +23 -0
  25. package/dist/esm/index.d.ts +5 -0
  26. package/dist/esm/index.js +5 -0
  27. package/dist/esm/mirror.d.ts +4 -0
  28. package/dist/esm/mirror.js +92 -0
  29. package/dist/esm/runtime.d.ts +9 -0
  30. package/dist/esm/runtime.js +4 -0
  31. package/dist/esm/storage.d.ts +8 -0
  32. package/dist/esm/storage.js +41 -0
  33. package/dist/esm/topLevel.d.ts +17 -0
  34. package/dist/esm/topLevel.js +27 -0
  35. package/dist/esm/types.d.ts +10 -0
  36. package/dist/esm/types.js +1 -0
  37. package/dist/esm/utils.d.ts +2 -0
  38. package/dist/esm/utils.js +8 -0
  39. package/package.json +15 -9
  40. package/dist/auth.js +0 -18
  41. package/dist/http.d.ts +0 -2
  42. package/dist/http.js +0 -23
  43. package/dist/mirror.d.ts +0 -3
  44. package/dist/types.d.ts +0 -3
  45. /package/dist/{cli.d.ts → cjs/cli.d.ts} +0 -0
  46. /package/dist/{cli.js → cjs/cli.js} +0 -0
  47. /package/dist/{types.js → cjs/types.js} +0 -0
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')
@@ -11,4 +11,5 @@ export type AuthenticationPayload = {
11
11
  password: string;
12
12
  };
13
13
  export declare const authMemo: AuthenticationResult;
14
- export declare const authenticate: (config: InstanceConfig) => (payload: AuthenticationPayload) => Promise<unknown>;
14
+ export declare const authenticate: (config: InstanceConfig) => (payload: AuthenticationPayload) => Promise<any>;
15
+ export declare const signout: (config: InstanceConfig) => () => Promise<void>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signout = exports.authenticate = exports.authMemo = void 0;
4
+ const common_1 = require("@sonata-api/common");
5
+ const http_1 = require("./http");
6
+ const utils_1 = require("./utils");
7
+ const storage_1 = require("./storage");
8
+ exports.authMemo = {};
9
+ const authenticate = (config) => async (payload) => {
10
+ const response = await (0, http_1.request)(config, `${(0, utils_1.apiUrl)(config)}/user/authenticate`, payload);
11
+ const resultEither = response.data;
12
+ if ((0, common_1.isRight)(resultEither)) {
13
+ const result = (0, common_1.unwrapEither)(resultEither);
14
+ (0, storage_1.getStorage)(config).set('auth', JSON.stringify(result));
15
+ }
16
+ return resultEither;
17
+ };
18
+ exports.authenticate = authenticate;
19
+ const signout = (config) => async () => {
20
+ (0, storage_1.getStorage)(config).remove('auth');
21
+ };
22
+ exports.signout = signout;
@@ -0,0 +1,3 @@
1
+ import type { InstanceConfig } from './types';
2
+ import { type RequestConfig } from '@sonata-api/common';
3
+ export declare const request: <Return = any>(config: InstanceConfig, url: string, payload?: any, _requestConfig?: RequestConfig) => Promise<Return>;
@@ -0,0 +1,27 @@
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 storage_1 = require("./storage");
6
+ const request = (config, url, payload, _requestConfig) => {
7
+ const requestConfig = Object.assign({}, _requestConfig);
8
+ requestConfig.requestTransformer ??= async (url, payload, _params) => {
9
+ const params = Object.assign({}, _params);
10
+ const authVal = (0, storage_1.getStorage)(config).get('auth');
11
+ const auth = authVal
12
+ ? JSON.parse(authVal)
13
+ : {};
14
+ if (auth.token) {
15
+ params.headers ??= {};
16
+ switch (auth.token.type) {
17
+ case 'bearer': {
18
+ params.headers.authorization = `Bearer ${auth.token.content}`;
19
+ break;
20
+ }
21
+ }
22
+ }
23
+ return (0, common_1.defaultRequestTransformer)(url, payload, params);
24
+ };
25
+ return (0, common_1.request)(url, payload, requestConfig);
26
+ };
27
+ exports.request = request;
@@ -1,4 +1,5 @@
1
1
  export { topLevel as Aeria } from './topLevel';
2
2
  export * from '@sonata-api/common';
3
3
  export * from './topLevel';
4
- export * from '.aeria-sdk';
4
+ export * from './runtime';
5
+ export * from './storage';
@@ -19,5 +19,5 @@ 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
21
  __exportStar(require("./topLevel"), exports);
22
- // @ts-ignore
23
- __exportStar(require(".aeria-sdk"), exports);
22
+ __exportStar(require("./runtime"), exports);
23
+ __exportStar(require("./storage"), exports);
@@ -0,0 +1,4 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const runtimeCjs: (config: InstanceConfig) => string;
3
+ export declare const runtimeEsm: (config: InstanceConfig) => string;
4
+ export declare const mirror: (config: InstanceConfig) => Promise<void>;
@@ -3,15 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.mirror = exports.runtimeJs = void 0;
6
+ exports.mirror = exports.runtimeEsm = exports.runtimeCjs = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const common_1 = require("@sonata-api/common");
9
9
  const promises_1 = require("fs/promises");
10
10
  const topLevel_1 = require("./topLevel");
11
+ const utils_1 = require("./utils");
11
12
  const mirrorDts = (mirrorObj) => {
12
13
  const collections = mirrorObj.descriptions;
13
14
  return `import type {
14
15
  InferSchema,
16
+ InferResponse,
15
17
  SchemaWithId,
16
18
  MakeEndpoint,
17
19
  CollectionDocument,
@@ -49,9 +51,11 @@ declare module 'aeria-sdk' {
49
51
  [Route in keyof MirrorRouter]: MirrorRouter[Route] extends infer RouteContract
50
52
  ? RouteContract extends [infer RoutePayload, infer RouteResponse]
51
53
  ? RoutePayload extends null
52
- ? MakeEndpoint<Route, InferSchema<RouteResponse>, undefined>
53
- : MakeEndpoint<Route, InferSchema<RouteResponse>, InferSchema<RoutePayload>>
54
- : MakeEndpoint<Route>
54
+ ? MakeEndpoint<Route, InferResponse<RouteResponse>, undefined>
55
+ : MakeEndpoint<Route, InferResponse<RouteResponse>, InferSchema<RoutePayload>>
56
+ : RouteContract extends Record<string, any>
57
+ ? MakeEndpoint<Route, any, InferSchema<RouteContract>>
58
+ : MakeEndpoint<Route>
55
59
  : never
56
60
  } extends infer Endpoints
57
61
  ? UnionToIntersection<Endpoints[keyof Endpoints]>
@@ -68,18 +72,30 @@ declare module 'aeria-sdk' {
68
72
  }\n
69
73
  `;
70
74
  };
71
- const runtimeJs = (config) => `exports.url = '${config.apiUrl}'
72
- exports.aeria = require('aeria-sdk').Aeria(${JSON.stringify(config)})\n
73
- `;
74
- exports.runtimeJs = runtimeJs;
75
+ const runtimeCjs = (config) => `const { Aeria, getStorage } from 'aeria-sdk'
76
+ const config = ${JSON.stringify(config)}
77
+ exports.config = config
78
+ exports.url = '${(0, utils_1.apiUrl)(config)}'
79
+ exports.aeria = Aeria(config)
80
+ exports.storage = getStorage(config)
81
+ \n`;
82
+ exports.runtimeCjs = runtimeCjs;
83
+ const runtimeEsm = (config) => `import { Aeria, getStorage } from 'aeria-sdk'
84
+ export const config = ${JSON.stringify(config)}
85
+ export const url = '${(0, utils_1.apiUrl)(config)}'
86
+ export const aeria = Aeria(config)
87
+ export const storage = getStorage(config)
88
+ \n`;
89
+ exports.runtimeEsm = runtimeEsm;
75
90
  const mirror = async (config) => {
76
91
  const api = (0, topLevel_1.topLevel)(config);
77
- const runtimeBase = path_1.default.join(process.cwd(), 'node_modules', '.aeria-sdk');
92
+ const runtimeBase = path_1.default.dirname(require.resolve('aeria-sdk'));
78
93
  const mirror = (0, common_1.deserialize)(await api.describe({
79
94
  router: true
80
95
  }));
81
96
  await (0, promises_1.mkdir)(runtimeBase, { recursive: true });
82
97
  await (0, promises_1.writeFile)(path_1.default.join(process.cwd(), 'aeria-sdk.d.ts'), mirrorDts(mirror));
83
- await (0, promises_1.writeFile)(path_1.default.join(runtimeBase, 'index.js'), (0, exports.runtimeJs)(config));
98
+ await (0, promises_1.writeFile)(path_1.default.join(runtimeBase, '..', 'cjs', 'runtime.js'), (0, exports.runtimeCjs)(config));
99
+ await (0, promises_1.writeFile)(path_1.default.join(runtimeBase, '..', 'esm', 'runtime.js'), (0, exports.runtimeEsm)(config));
84
100
  };
85
101
  exports.mirror = mirror;
@@ -0,0 +1,9 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const instanceConfig: InstanceConfig;
3
+ export declare const url = "";
4
+ export declare const aeria: {};
5
+ export declare const storage: {
6
+ get: (key: string) => string | null;
7
+ remove: (key: string) => void;
8
+ set: (key: string, value: string) => void;
9
+ };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.storage = exports.aeria = exports.url = exports.instanceConfig = void 0;
4
+ exports.instanceConfig = {};
5
+ exports.url = '';
6
+ exports.aeria = {};
7
+ exports.storage = {};
@@ -0,0 +1,8 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const storageMemo: Record<string, string>;
3
+ export declare const storageKey: (key: string, config: InstanceConfig) => string;
4
+ export declare const getStorage: (config: InstanceConfig) => {
5
+ get: (key: string) => string | null;
6
+ remove: (key: string) => void;
7
+ set: (key: string, value: string) => void;
8
+ };
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStorage = exports.storageKey = exports.storageMemo = void 0;
4
+ exports.storageMemo = {};
5
+ const storageKey = (key, config) => {
6
+ const namespace = config.storage?.namespace || 'aeriaSdk';
7
+ return `${namespace}:${key}`;
8
+ };
9
+ exports.storageKey = storageKey;
10
+ const getStorage = (config) => {
11
+ const strategy = !config.storage?.strategy
12
+ ? 'memo'
13
+ : config.storage.strategy === 'localStorage' && !('localStorage' in globalThis)
14
+ ? 'memo'
15
+ : config.storage.strategy;
16
+ return {
17
+ get: (key) => {
18
+ switch (strategy) {
19
+ case 'memo':
20
+ return exports.storageMemo[key];
21
+ case 'localStorage':
22
+ return localStorage.getItem((0, exports.storageKey)(key, config));
23
+ }
24
+ },
25
+ remove: (key) => {
26
+ switch (strategy) {
27
+ case 'memo':
28
+ delete exports.storageMemo[key];
29
+ break;
30
+ case 'localStorage':
31
+ localStorage.removeItem((0, exports.storageKey)(key, config));
32
+ break;
33
+ }
34
+ },
35
+ set: (key, value) => {
36
+ switch (strategy) {
37
+ case 'memo':
38
+ exports.storageMemo[key] = value;
39
+ break;
40
+ case 'localStorage':
41
+ return localStorage.setItem((0, exports.storageKey)(key, config), value);
42
+ }
43
+ },
44
+ };
45
+ };
46
+ exports.getStorage = getStorage;
@@ -3,6 +3,7 @@ import { type AuthenticationPayload } from './auth';
3
3
  type UserFunctions = {
4
4
  user: TLOFunctions & {
5
5
  authenticate: (payload: AuthenticationPayload) => Promise<any>;
6
+ signout: () => Promise<void>;
6
7
  };
7
8
  };
8
9
  export type TLOFunctions = {
@@ -10,6 +11,7 @@ export type TLOFunctions = {
10
11
  };
11
12
  export type TopLevelObject = UserFunctions & {
12
13
  describe: (...args: any) => Promise<any>;
14
+ $currentUser: any;
13
15
  };
14
16
  export declare const topLevel: (config: InstanceConfig) => TopLevelObject;
15
17
  export {};
@@ -3,19 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.topLevel = void 0;
4
4
  const auth_1 = require("./auth");
5
5
  const http_1 = require("./http");
6
+ const utils_1 = require("./utils");
6
7
  const topLevel = (config) => {
7
8
  const proxify = (target, parent) => new Proxy(target, {
8
9
  get: (_, key) => {
10
+ if (typeof key === 'symbol') {
11
+ return target[key];
12
+ }
9
13
  const endpoint = parent
10
14
  ? `${parent}/${key}`
11
15
  : `${key}`;
12
16
  switch (endpoint) {
13
17
  case 'user/authenticate': return (0, auth_1.authenticate)(config);
18
+ case 'user/signout': return (0, auth_1.signout)(config);
14
19
  }
15
20
  const fn = async (payload) => {
16
21
  const response = payload
17
- ? await (0, http_1.request)(`${config.apiUrl}/${endpoint}`, payload)
18
- : await (0, http_1.request)(`${config.apiUrl}/${endpoint}`);
22
+ ? await (0, http_1.request)(config, `${(0, utils_1.apiUrl)(config)}/${endpoint}`, payload)
23
+ : await (0, http_1.request)(config, `${(0, utils_1.apiUrl)(config)}/${endpoint}`);
19
24
  return response.data;
20
25
  };
21
26
  return proxify(fn, endpoint);
@@ -24,12 +29,3 @@ const topLevel = (config) => {
24
29
  return proxify({});
25
30
  };
26
31
  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,10 @@
1
+ export type InstanceConfig = {
2
+ apiUrl: string | {
3
+ production: string;
4
+ development: string;
5
+ };
6
+ storage?: {
7
+ strategy?: 'memo' | 'localStorage';
8
+ namespace?: string;
9
+ };
10
+ };
@@ -0,0 +1,2 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const apiUrl: (config: InstanceConfig) => string;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.apiUrl = void 0;
4
+ const apiUrl = (config) => {
5
+ if (typeof config.apiUrl === 'string') {
6
+ return config.apiUrl;
7
+ }
8
+ return process.env.NODE_ENV === 'production'
9
+ ? config.apiUrl.production
10
+ : config.apiUrl.development;
11
+ };
12
+ exports.apiUrl = apiUrl;
@@ -0,0 +1,15 @@
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<any>;
15
+ export declare const signout: (config: InstanceConfig) => () => Promise<void>;
@@ -0,0 +1,17 @@
1
+ import { isRight, unwrapEither } from '@sonata-api/common';
2
+ import { request } from './http';
3
+ import { apiUrl } from './utils';
4
+ import { getStorage } from './storage';
5
+ export const authMemo = {};
6
+ export const authenticate = (config) => async (payload) => {
7
+ const response = await request(config, `${apiUrl(config)}/user/authenticate`, payload);
8
+ const resultEither = response.data;
9
+ if (isRight(resultEither)) {
10
+ const result = unwrapEither(resultEither);
11
+ getStorage(config).set('auth', JSON.stringify(result));
12
+ }
13
+ return resultEither;
14
+ };
15
+ export const signout = (config) => async () => {
16
+ getStorage(config).remove('auth');
17
+ };
@@ -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,3 @@
1
+ import type { InstanceConfig } from './types';
2
+ import { type RequestConfig } from '@sonata-api/common';
3
+ export declare const request: <Return = any>(config: InstanceConfig, url: string, payload?: any, _requestConfig?: RequestConfig) => Promise<Return>;
@@ -0,0 +1,23 @@
1
+ import { request as originalRequest, defaultRequestTransformer } from '@sonata-api/common';
2
+ import { getStorage } from './storage';
3
+ export const request = (config, url, payload, _requestConfig) => {
4
+ const requestConfig = Object.assign({}, _requestConfig);
5
+ requestConfig.requestTransformer ??= async (url, payload, _params) => {
6
+ const params = Object.assign({}, _params);
7
+ const authVal = getStorage(config).get('auth');
8
+ const auth = authVal
9
+ ? JSON.parse(authVal)
10
+ : {};
11
+ if (auth.token) {
12
+ params.headers ??= {};
13
+ switch (auth.token.type) {
14
+ case 'bearer': {
15
+ params.headers.authorization = `Bearer ${auth.token.content}`;
16
+ break;
17
+ }
18
+ }
19
+ }
20
+ return defaultRequestTransformer(url, payload, params);
21
+ };
22
+ return originalRequest(url, payload, requestConfig);
23
+ };
@@ -0,0 +1,5 @@
1
+ export { topLevel as Aeria } from './topLevel';
2
+ export * from '@sonata-api/common';
3
+ export * from './topLevel';
4
+ export * from './runtime';
5
+ export * from './storage';
@@ -0,0 +1,5 @@
1
+ export { topLevel as Aeria } from './topLevel';
2
+ export * from '@sonata-api/common';
3
+ export * from './topLevel';
4
+ export * from './runtime';
5
+ export * from './storage';
@@ -0,0 +1,4 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const runtimeCjs: (config: InstanceConfig) => string;
3
+ export declare const runtimeEsm: (config: InstanceConfig) => string;
4
+ export declare const mirror: (config: InstanceConfig) => Promise<void>;
@@ -0,0 +1,92 @@
1
+ import path from 'path';
2
+ import { deserialize } from '@sonata-api/common';
3
+ import { writeFile, mkdir } from 'fs/promises';
4
+ import { topLevel } from './topLevel';
5
+ import { apiUrl } from './utils';
6
+ const mirrorDts = (mirrorObj) => {
7
+ const collections = mirrorObj.descriptions;
8
+ return `import type {
9
+ InferSchema,
10
+ InferResponse,
11
+ SchemaWithId,
12
+ MakeEndpoint,
13
+ CollectionDocument,
14
+ GetPayload,
15
+ GetAllPayload,
16
+ InsertPayload,
17
+ RemovePayload,
18
+ RemoveAllPayload,
19
+ UploadPayload,
20
+ RemoveFilePayload,
21
+ CollectionFunctions
22
+
23
+ } from '@sonata-api/types'
24
+
25
+ declare type MirrorDescriptions = ${JSON.stringify(collections, null, 2)}\n
26
+
27
+ declare type MirrorRouter = ${JSON.stringify(mirrorObj.router, null, 2)}\n
28
+
29
+ declare global {
30
+ type Collections = {
31
+ [K in keyof MirrorDescriptions]: {
32
+ item: SchemaWithId<MirrorDescriptions[K]>
33
+ }
34
+ }
35
+ }
36
+
37
+ declare module 'aeria-sdk' {
38
+ import { TopLevelObject, TLOFunctions } from 'aeria-sdk'
39
+
40
+ type UnionToIntersection<T> = (T extends any ? ((x: T) => 0) : never) extends ((x: infer R) => 0)
41
+ ? R
42
+ : never
43
+
44
+ type Endpoints = {
45
+ [Route in keyof MirrorRouter]: MirrorRouter[Route] extends infer RouteContract
46
+ ? RouteContract extends [infer RoutePayload, infer RouteResponse]
47
+ ? RoutePayload extends null
48
+ ? MakeEndpoint<Route, InferResponse<RouteResponse>, undefined>
49
+ : MakeEndpoint<Route, InferResponse<RouteResponse>, InferSchema<RoutePayload>>
50
+ : RouteContract extends Record<string, any>
51
+ ? MakeEndpoint<Route, any, InferSchema<RouteContract>>
52
+ : MakeEndpoint<Route>
53
+ : never
54
+ } extends infer Endpoints
55
+ ? UnionToIntersection<Endpoints[keyof Endpoints]>
56
+ : never
57
+
58
+ type StrongelyTypedTLO = TopLevelObject & Endpoints & {
59
+ [K in keyof MirrorDescriptions]: SchemaWithId<MirrorDescriptions[K]> extends infer Document
60
+ ? CollectionFunctions<Document> & Omit<TLOFunctions, keyof Functions>
61
+ : never
62
+ }
63
+
64
+ export const url: string
65
+ export const aeria: StrongelyTypedTLO
66
+ }\n
67
+ `;
68
+ };
69
+ export const runtimeCjs = (config) => `const { Aeria, getStorage } from 'aeria-sdk'
70
+ const config = ${JSON.stringify(config)}
71
+ exports.config = config
72
+ exports.url = '${apiUrl(config)}'
73
+ exports.aeria = Aeria(config)
74
+ exports.storage = getStorage(config)
75
+ \n`;
76
+ export const runtimeEsm = (config) => `import { Aeria, getStorage } from 'aeria-sdk'
77
+ export const config = ${JSON.stringify(config)}
78
+ export const url = '${apiUrl(config)}'
79
+ export const aeria = Aeria(config)
80
+ export const storage = getStorage(config)
81
+ \n`;
82
+ export const mirror = async (config) => {
83
+ const api = topLevel(config);
84
+ const runtimeBase = path.dirname(require.resolve('aeria-sdk'));
85
+ const mirror = deserialize(await api.describe({
86
+ router: true
87
+ }));
88
+ await mkdir(runtimeBase, { recursive: true });
89
+ await writeFile(path.join(process.cwd(), 'aeria-sdk.d.ts'), mirrorDts(mirror));
90
+ await writeFile(path.join(runtimeBase, '..', 'cjs', 'runtime.js'), runtimeCjs(config));
91
+ await writeFile(path.join(runtimeBase, '..', 'esm', 'runtime.js'), runtimeEsm(config));
92
+ };
@@ -0,0 +1,9 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const instanceConfig: InstanceConfig;
3
+ export declare const url = "";
4
+ export declare const aeria: {};
5
+ export declare const storage: {
6
+ get: (key: string) => string | null;
7
+ remove: (key: string) => void;
8
+ set: (key: string, value: string) => void;
9
+ };
@@ -0,0 +1,4 @@
1
+ export const instanceConfig = {};
2
+ export const url = '';
3
+ export const aeria = {};
4
+ export const storage = {};
@@ -0,0 +1,8 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const storageMemo: Record<string, string>;
3
+ export declare const storageKey: (key: string, config: InstanceConfig) => string;
4
+ export declare const getStorage: (config: InstanceConfig) => {
5
+ get: (key: string) => string | null;
6
+ remove: (key: string) => void;
7
+ set: (key: string, value: string) => void;
8
+ };
@@ -0,0 +1,41 @@
1
+ export const storageMemo = {};
2
+ export const storageKey = (key, config) => {
3
+ const namespace = config.storage?.namespace || 'aeriaSdk';
4
+ return `${namespace}:${key}`;
5
+ };
6
+ export const getStorage = (config) => {
7
+ const strategy = !config.storage?.strategy
8
+ ? 'memo'
9
+ : config.storage.strategy === 'localStorage' && !('localStorage' in globalThis)
10
+ ? 'memo'
11
+ : config.storage.strategy;
12
+ return {
13
+ get: (key) => {
14
+ switch (strategy) {
15
+ case 'memo':
16
+ return storageMemo[key];
17
+ case 'localStorage':
18
+ return localStorage.getItem(storageKey(key, config));
19
+ }
20
+ },
21
+ remove: (key) => {
22
+ switch (strategy) {
23
+ case 'memo':
24
+ delete storageMemo[key];
25
+ break;
26
+ case 'localStorage':
27
+ localStorage.removeItem(storageKey(key, config));
28
+ break;
29
+ }
30
+ },
31
+ set: (key, value) => {
32
+ switch (strategy) {
33
+ case 'memo':
34
+ storageMemo[key] = value;
35
+ break;
36
+ case 'localStorage':
37
+ return localStorage.setItem(storageKey(key, config), value);
38
+ }
39
+ },
40
+ };
41
+ };
@@ -0,0 +1,17 @@
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
+ signout: () => Promise<void>;
7
+ };
8
+ };
9
+ export type TLOFunctions = {
10
+ [P in string]: ((payload?: any) => Promise<any>) & TLOFunctions;
11
+ };
12
+ export type TopLevelObject = UserFunctions & {
13
+ describe: (...args: any) => Promise<any>;
14
+ $currentUser: any;
15
+ };
16
+ export declare const topLevel: (config: InstanceConfig) => TopLevelObject;
17
+ export {};
@@ -0,0 +1,27 @@
1
+ import { authenticate, signout } from './auth';
2
+ import { request } from './http';
3
+ import { apiUrl } from './utils';
4
+ export const topLevel = (config) => {
5
+ const proxify = (target, parent) => new Proxy(target, {
6
+ get: (_, key) => {
7
+ if (typeof key === 'symbol') {
8
+ return target[key];
9
+ }
10
+ const endpoint = parent
11
+ ? `${parent}/${key}`
12
+ : `${key}`;
13
+ switch (endpoint) {
14
+ case 'user/authenticate': return authenticate(config);
15
+ case 'user/signout': return signout(config);
16
+ }
17
+ const fn = async (payload) => {
18
+ const response = payload
19
+ ? await request(config, `${apiUrl(config)}/${endpoint}`, payload)
20
+ : await request(config, `${apiUrl(config)}/${endpoint}`);
21
+ return response.data;
22
+ };
23
+ return proxify(fn, endpoint);
24
+ }
25
+ });
26
+ return proxify({});
27
+ };
@@ -0,0 +1,10 @@
1
+ export type InstanceConfig = {
2
+ apiUrl: string | {
3
+ production: string;
4
+ development: string;
5
+ };
6
+ storage?: {
7
+ strategy?: 'memo' | 'localStorage';
8
+ namespace?: string;
9
+ };
10
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { InstanceConfig } from './types';
2
+ export declare const apiUrl: (config: InstanceConfig) => string;
@@ -0,0 +1,8 @@
1
+ export const apiUrl = (config) => {
2
+ if (typeof config.apiUrl === 'string') {
3
+ return config.apiUrl;
4
+ }
5
+ return process.env.NODE_ENV === 'production'
6
+ ? config.apiUrl.production
7
+ : config.apiUrl.development;
8
+ };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "aeria-sdk",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
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/auth.js DELETED
@@ -1,18 +0,0 @@
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/http.d.ts DELETED
@@ -1,2 +0,0 @@
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 DELETED
@@ -1,23 +0,0 @@
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;
package/dist/mirror.d.ts DELETED
@@ -1,3 +0,0 @@
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/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