aeria-sdk 0.0.6 → 0.0.7

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.
@@ -1,6 +1,6 @@
1
1
  import type { InstanceConfig } from './types';
2
2
  export type AuthenticationResult = {
3
- user: any;
3
+ user: Collections['user']['item'];
4
4
  token: {
5
5
  type: 'bearer';
6
6
  content: string;
package/dist/cjs/auth.js CHANGED
@@ -11,7 +11,7 @@ const authenticate = (config) => async (payload) => {
11
11
  const resultEither = response.data;
12
12
  if ((0, common_1.isRight)(resultEither)) {
13
13
  const result = (0, common_1.unwrapEither)(resultEither);
14
- (0, storage_1.getStorage)(config).set('auth', JSON.stringify(result));
14
+ (0, storage_1.getStorage)(config).set('auth', result);
15
15
  }
16
16
  return resultEither;
17
17
  };
package/dist/cjs/http.js CHANGED
@@ -7,10 +7,7 @@ const request = (config, url, payload, _requestConfig) => {
7
7
  const requestConfig = Object.assign({}, _requestConfig);
8
8
  requestConfig.requestTransformer ??= async (url, payload, _params) => {
9
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
- : {};
10
+ const auth = (0, storage_1.getStorage)(config).get('auth') || {};
14
11
  if (auth.token) {
15
12
  params.headers ??= {};
16
13
  switch (auth.token.type) {
@@ -12,18 +12,11 @@ const utils_1 = require("./utils");
12
12
  const mirrorDts = (mirrorObj) => {
13
13
  const collections = mirrorObj.descriptions;
14
14
  return `import type {
15
- InferSchema,
15
+ InferProperty,
16
16
  InferResponse,
17
17
  SchemaWithId,
18
18
  MakeEndpoint,
19
- CollectionDocument,
20
- GetPayload,
21
- GetAllPayload,
22
- InsertPayload,
23
- RemovePayload,
24
- RemoveAllPayload,
25
- UploadPayload,
26
- RemoveFilePayload,
19
+ RequestMethod,
27
20
  CollectionFunctions
28
21
 
29
22
  } from '@sonata-api/types'
@@ -48,14 +41,19 @@ declare module 'aeria-sdk' {
48
41
  : never
49
42
 
50
43
  type Endpoints = {
51
- [Route in keyof MirrorRouter]: MirrorRouter[Route] extends infer RouteContract
52
- ? RouteContract extends [infer RoutePayload, infer RouteResponse]
53
- ? RoutePayload extends null
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>
44
+ [Route in keyof MirrorRouter]: {
45
+ [Method in keyof MirrorRouter[Route]]: Method extends RequestMethod
46
+ ? MirrorRouter[Route][Method] extends infer RouteContract
47
+ ? RouteContract extends
48
+ | { response: infer RouteResponse }
49
+ | { payload: infer RoutePayload }
50
+ | { query: infer RoutePayload }
51
+ ? MakeEndpoint<Route, Method, InferResponse<RouteResponse>, InferProperty<RoutePayload>>
52
+ : MakeEndpoint<Route, Method>
53
+ : never
54
+ : never
55
+ } extends infer Methods
56
+ ? Methods[keyof Methods]
59
57
  : never
60
58
  } extends infer Endpoints
61
59
  ? UnionToIntersection<Endpoints[keyof Endpoints]>
@@ -63,7 +61,13 @@ declare module 'aeria-sdk' {
63
61
 
64
62
  type StrongelyTypedTLO = TopLevelObject & Endpoints & {
65
63
  [K in keyof MirrorDescriptions]: SchemaWithId<MirrorDescriptions[K]> extends infer Document
66
- ? CollectionFunctions<Document> & Omit<TLOFunctions, keyof Functions>
64
+ ? CollectionFunctions<Document> extends infer Functions
65
+ ? Omit<TLOFunctions, keyof Functions> & {
66
+ [P in keyof Functions]: {
67
+ POST: Functions[P]
68
+ }
69
+ }
70
+ : never
67
71
  : never
68
72
  }
69
73
 
@@ -72,12 +76,11 @@ declare module 'aeria-sdk' {
72
76
  }\n
73
77
  `;
74
78
  };
75
- const runtimeCjs = (config) => `const { Aeria, getStorage } from 'aeria-sdk'
76
- const config = ${JSON.stringify(config)}
79
+ const runtimeCjs = (config) => `const config = ${JSON.stringify(config)}
77
80
  exports.config = config
78
81
  exports.url = '${(0, utils_1.apiUrl)(config)}'
79
- exports.aeria = Aeria(config)
80
- exports.storage = getStorage(config)
82
+ exports.aeria = require('aeria-sdk/topLevel').topLevel(config)
83
+ exports.storage = require('aeria-sdk/storage').getStorage(config)
81
84
  \n`;
82
85
  exports.runtimeCjs = runtimeCjs;
83
86
  const runtimeEsm = (config) => `import { Aeria, getStorage } from 'aeria-sdk'
@@ -90,7 +93,7 @@ exports.runtimeEsm = runtimeEsm;
90
93
  const mirror = async (config) => {
91
94
  const api = (0, topLevel_1.topLevel)(config);
92
95
  const runtimeBase = path_1.default.dirname(require.resolve('aeria-sdk'));
93
- const mirror = (0, common_1.deserialize)(await api.describe({
96
+ const mirror = (0, common_1.deserialize)(await api.describe.POST({
94
97
  router: true
95
98
  }));
96
99
  await (0, promises_1.mkdir)(runtimeBase, { recursive: true });
@@ -3,7 +3,7 @@ export declare const instanceConfig: InstanceConfig;
3
3
  export declare const url = "";
4
4
  export declare const aeria: {};
5
5
  export declare const storage: {
6
- get: (key: string) => string | null;
6
+ get: (key: "auth") => import("./auth").AuthenticationResult;
7
7
  remove: (key: string) => void;
8
- set: (key: string, value: string) => void;
8
+ set: (key: string, value: any) => void;
9
9
  };
@@ -1,8 +1,9 @@
1
1
  import type { InstanceConfig } from './types';
2
+ import type { AuthenticationResult } from './auth';
2
3
  export declare const storageMemo: Record<string, string>;
3
4
  export declare const storageKey: (key: string, config: InstanceConfig) => string;
4
5
  export declare const getStorage: (config: InstanceConfig) => {
5
- get: (key: string) => string | null;
6
+ get: (key: 'auth') => AuthenticationResult;
6
7
  remove: (key: string) => void;
7
- set: (key: string, value: string) => void;
8
+ set: (key: string, value: any) => void;
8
9
  };
@@ -13,15 +13,19 @@ const getStorage = (config) => {
13
13
  : config.storage.strategy === 'localStorage' && !('localStorage' in globalThis)
14
14
  ? 'memo'
15
15
  : config.storage.strategy;
16
+ function get(key) {
17
+ switch (strategy) {
18
+ case 'memo':
19
+ return exports.storageMemo[key];
20
+ case 'localStorage':
21
+ const value = localStorage.getItem((0, exports.storageKey)(key, config));
22
+ return value
23
+ ? JSON.parse(value)
24
+ : null;
25
+ }
26
+ }
16
27
  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
- },
28
+ get,
25
29
  remove: (key) => {
26
30
  switch (strategy) {
27
31
  case 'memo':
@@ -38,7 +42,8 @@ const getStorage = (config) => {
38
42
  exports.storageMemo[key] = value;
39
43
  break;
40
44
  case 'localStorage':
41
- return localStorage.setItem((0, exports.storageKey)(key, config), value);
45
+ const serialized = JSON.stringify(value);
46
+ return localStorage.setItem((0, exports.storageKey)(key, config), serialized);
42
47
  }
43
48
  },
44
49
  };
@@ -1,4 +1,5 @@
1
1
  import type { InstanceConfig } from './types';
2
+ import { RequestMethod } from '@sonata-api/types';
2
3
  import { type AuthenticationPayload } from './auth';
3
4
  type UserFunctions = {
4
5
  user: TLOFunctions & {
@@ -7,11 +8,12 @@ type UserFunctions = {
7
8
  };
8
9
  };
9
10
  export type TLOFunctions = {
10
- [P in string]: ((payload?: any) => Promise<any>) & TLOFunctions;
11
+ [P: string]: Record<RequestMethod, ((payload?: any) => Promise<any>) & TLOFunctions>;
11
12
  };
12
13
  export type TopLevelObject = UserFunctions & {
13
- describe: (...args: any) => Promise<any>;
14
- $currentUser: any;
14
+ describe: {
15
+ POST: (...args: any) => Promise<any>;
16
+ };
15
17
  };
16
18
  export declare const topLevel: (config: InstanceConfig) => TopLevelObject;
17
19
  export {};
@@ -10,20 +10,32 @@ const topLevel = (config) => {
10
10
  if (typeof key === 'symbol') {
11
11
  return target[key];
12
12
  }
13
- const endpoint = parent
14
- ? `${parent}/${key}`
15
- : `${key}`;
16
- switch (endpoint) {
13
+ switch (`${parent}/${key}`) {
17
14
  case 'user/authenticate': return (0, auth_1.authenticate)(config);
18
15
  case 'user/signout': return (0, auth_1.signout)(config);
19
16
  }
17
+ const endpoint = parent;
20
18
  const fn = async (payload) => {
21
- const response = payload
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
+ const method = key;
20
+ const requestConfig = {
21
+ params: {
22
+ method
23
+ }
24
+ };
25
+ if (method !== 'GET' && method !== 'HEAD') {
26
+ if (payload) {
27
+ requestConfig.params.headers = {
28
+ 'content-type': 'application/json'
29
+ };
30
+ }
31
+ }
32
+ const response = await (0, http_1.request)(config, `${(0, utils_1.apiUrl)(config)}/${endpoint}`, payload, requestConfig);
24
33
  return response.data;
25
34
  };
26
- return proxify(fn, endpoint);
35
+ const path = parent
36
+ ? `${parent}/${key}`
37
+ : key;
38
+ return proxify(fn, path);
27
39
  }
28
40
  });
29
41
  return proxify({});
@@ -1,6 +1,6 @@
1
1
  import type { InstanceConfig } from './types';
2
2
  export type AuthenticationResult = {
3
- user: any;
3
+ user: Collections['user']['item'];
4
4
  token: {
5
5
  type: 'bearer';
6
6
  content: string;
package/dist/esm/auth.js CHANGED
@@ -8,7 +8,7 @@ export const authenticate = (config) => async (payload) => {
8
8
  const resultEither = response.data;
9
9
  if (isRight(resultEither)) {
10
10
  const result = unwrapEither(resultEither);
11
- getStorage(config).set('auth', JSON.stringify(result));
11
+ getStorage(config).set('auth', result);
12
12
  }
13
13
  return resultEither;
14
14
  };
package/dist/esm/http.js CHANGED
@@ -4,10 +4,7 @@ export const request = (config, url, payload, _requestConfig) => {
4
4
  const requestConfig = Object.assign({}, _requestConfig);
5
5
  requestConfig.requestTransformer ??= async (url, payload, _params) => {
6
6
  const params = Object.assign({}, _params);
7
- const authVal = getStorage(config).get('auth');
8
- const auth = authVal
9
- ? JSON.parse(authVal)
10
- : {};
7
+ const auth = getStorage(config).get('auth') || {};
11
8
  if (auth.token) {
12
9
  params.headers ??= {};
13
10
  switch (auth.token.type) {
@@ -6,18 +6,11 @@ import { apiUrl } from './utils';
6
6
  const mirrorDts = (mirrorObj) => {
7
7
  const collections = mirrorObj.descriptions;
8
8
  return `import type {
9
- InferSchema,
9
+ InferProperty,
10
10
  InferResponse,
11
11
  SchemaWithId,
12
12
  MakeEndpoint,
13
- CollectionDocument,
14
- GetPayload,
15
- GetAllPayload,
16
- InsertPayload,
17
- RemovePayload,
18
- RemoveAllPayload,
19
- UploadPayload,
20
- RemoveFilePayload,
13
+ RequestMethod,
21
14
  CollectionFunctions
22
15
 
23
16
  } from '@sonata-api/types'
@@ -42,14 +35,19 @@ declare module 'aeria-sdk' {
42
35
  : never
43
36
 
44
37
  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>
38
+ [Route in keyof MirrorRouter]: {
39
+ [Method in keyof MirrorRouter[Route]]: Method extends RequestMethod
40
+ ? MirrorRouter[Route][Method] extends infer RouteContract
41
+ ? RouteContract extends
42
+ | { response: infer RouteResponse }
43
+ | { payload: infer RoutePayload }
44
+ | { query: infer RoutePayload }
45
+ ? MakeEndpoint<Route, Method, InferResponse<RouteResponse>, InferProperty<RoutePayload>>
46
+ : MakeEndpoint<Route, Method>
47
+ : never
48
+ : never
49
+ } extends infer Methods
50
+ ? Methods[keyof Methods]
53
51
  : never
54
52
  } extends infer Endpoints
55
53
  ? UnionToIntersection<Endpoints[keyof Endpoints]>
@@ -57,7 +55,13 @@ declare module 'aeria-sdk' {
57
55
 
58
56
  type StrongelyTypedTLO = TopLevelObject & Endpoints & {
59
57
  [K in keyof MirrorDescriptions]: SchemaWithId<MirrorDescriptions[K]> extends infer Document
60
- ? CollectionFunctions<Document> & Omit<TLOFunctions, keyof Functions>
58
+ ? CollectionFunctions<Document> extends infer Functions
59
+ ? Omit<TLOFunctions, keyof Functions> & {
60
+ [P in keyof Functions]: {
61
+ POST: Functions[P]
62
+ }
63
+ }
64
+ : never
61
65
  : never
62
66
  }
63
67
 
@@ -66,12 +70,11 @@ declare module 'aeria-sdk' {
66
70
  }\n
67
71
  `;
68
72
  };
69
- export const runtimeCjs = (config) => `const { Aeria, getStorage } from 'aeria-sdk'
70
- const config = ${JSON.stringify(config)}
73
+ export const runtimeCjs = (config) => `const config = ${JSON.stringify(config)}
71
74
  exports.config = config
72
75
  exports.url = '${apiUrl(config)}'
73
- exports.aeria = Aeria(config)
74
- exports.storage = getStorage(config)
76
+ exports.aeria = require('aeria-sdk/topLevel').topLevel(config)
77
+ exports.storage = require('aeria-sdk/storage').getStorage(config)
75
78
  \n`;
76
79
  export const runtimeEsm = (config) => `import { Aeria, getStorage } from 'aeria-sdk'
77
80
  export const config = ${JSON.stringify(config)}
@@ -82,7 +85,7 @@ export const storage = getStorage(config)
82
85
  export const mirror = async (config) => {
83
86
  const api = topLevel(config);
84
87
  const runtimeBase = path.dirname(require.resolve('aeria-sdk'));
85
- const mirror = deserialize(await api.describe({
88
+ const mirror = deserialize(await api.describe.POST({
86
89
  router: true
87
90
  }));
88
91
  await mkdir(runtimeBase, { recursive: true });
@@ -3,7 +3,7 @@ export declare const instanceConfig: InstanceConfig;
3
3
  export declare const url = "";
4
4
  export declare const aeria: {};
5
5
  export declare const storage: {
6
- get: (key: string) => string | null;
6
+ get: (key: "auth") => import("./auth").AuthenticationResult;
7
7
  remove: (key: string) => void;
8
- set: (key: string, value: string) => void;
8
+ set: (key: string, value: any) => void;
9
9
  };
@@ -1,8 +1,9 @@
1
1
  import type { InstanceConfig } from './types';
2
+ import type { AuthenticationResult } from './auth';
2
3
  export declare const storageMemo: Record<string, string>;
3
4
  export declare const storageKey: (key: string, config: InstanceConfig) => string;
4
5
  export declare const getStorage: (config: InstanceConfig) => {
5
- get: (key: string) => string | null;
6
+ get: (key: 'auth') => AuthenticationResult;
6
7
  remove: (key: string) => void;
7
- set: (key: string, value: string) => void;
8
+ set: (key: string, value: any) => void;
8
9
  };
@@ -9,15 +9,19 @@ export const getStorage = (config) => {
9
9
  : config.storage.strategy === 'localStorage' && !('localStorage' in globalThis)
10
10
  ? 'memo'
11
11
  : config.storage.strategy;
12
+ function get(key) {
13
+ switch (strategy) {
14
+ case 'memo':
15
+ return storageMemo[key];
16
+ case 'localStorage':
17
+ const value = localStorage.getItem(storageKey(key, config));
18
+ return value
19
+ ? JSON.parse(value)
20
+ : null;
21
+ }
22
+ }
12
23
  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
- },
24
+ get,
21
25
  remove: (key) => {
22
26
  switch (strategy) {
23
27
  case 'memo':
@@ -34,7 +38,8 @@ export const getStorage = (config) => {
34
38
  storageMemo[key] = value;
35
39
  break;
36
40
  case 'localStorage':
37
- return localStorage.setItem(storageKey(key, config), value);
41
+ const serialized = JSON.stringify(value);
42
+ return localStorage.setItem(storageKey(key, config), serialized);
38
43
  }
39
44
  },
40
45
  };
@@ -1,4 +1,5 @@
1
1
  import type { InstanceConfig } from './types';
2
+ import { RequestMethod } from '@sonata-api/types';
2
3
  import { type AuthenticationPayload } from './auth';
3
4
  type UserFunctions = {
4
5
  user: TLOFunctions & {
@@ -7,11 +8,12 @@ type UserFunctions = {
7
8
  };
8
9
  };
9
10
  export type TLOFunctions = {
10
- [P in string]: ((payload?: any) => Promise<any>) & TLOFunctions;
11
+ [P: string]: Record<RequestMethod, ((payload?: any) => Promise<any>) & TLOFunctions>;
11
12
  };
12
13
  export type TopLevelObject = UserFunctions & {
13
- describe: (...args: any) => Promise<any>;
14
- $currentUser: any;
14
+ describe: {
15
+ POST: (...args: any) => Promise<any>;
16
+ };
15
17
  };
16
18
  export declare const topLevel: (config: InstanceConfig) => TopLevelObject;
17
19
  export {};
@@ -7,20 +7,32 @@ export const topLevel = (config) => {
7
7
  if (typeof key === 'symbol') {
8
8
  return target[key];
9
9
  }
10
- const endpoint = parent
11
- ? `${parent}/${key}`
12
- : `${key}`;
13
- switch (endpoint) {
10
+ switch (`${parent}/${key}`) {
14
11
  case 'user/authenticate': return authenticate(config);
15
12
  case 'user/signout': return signout(config);
16
13
  }
14
+ const endpoint = parent;
17
15
  const fn = async (payload) => {
18
- const response = payload
19
- ? await request(config, `${apiUrl(config)}/${endpoint}`, payload)
20
- : await request(config, `${apiUrl(config)}/${endpoint}`);
16
+ const method = key;
17
+ const requestConfig = {
18
+ params: {
19
+ method
20
+ }
21
+ };
22
+ if (method !== 'GET' && method !== 'HEAD') {
23
+ if (payload) {
24
+ requestConfig.params.headers = {
25
+ 'content-type': 'application/json'
26
+ };
27
+ }
28
+ }
29
+ const response = await request(config, `${apiUrl(config)}/${endpoint}`, payload, requestConfig);
21
30
  return response.data;
22
31
  };
23
- return proxify(fn, endpoint);
32
+ const path = parent
33
+ ? `${parent}/${key}`
34
+ : key;
35
+ return proxify(fn, path);
24
36
  }
25
37
  });
26
38
  return proxify({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aeria-sdk",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/cjs/index.d.ts",
@@ -21,15 +21,20 @@
21
21
  ".": {
22
22
  "import": "./dist/esm/index.js",
23
23
  "require": "./dist/cjs/index.js"
24
+ },
25
+ "./storage": {
26
+ "import": "./dist/esm/storage.js",
27
+ "require": "./dist/cjs/storage.js"
28
+ },
29
+ "./topLevel": {
30
+ "import": "./dist/esm/topLevel.js",
31
+ "require": "./dist/cjs/topLevel.js"
24
32
  }
25
33
  },
26
34
  "peerDependencies": {
27
35
  "@sonata-api/common": "*",
28
36
  "@sonata-api/types": "*"
29
37
  },
30
- "devDependencies": {
31
- "@types/node": "^20.10.4"
32
- },
33
38
  "scripts": {
34
39
  "test": "echo skipping",
35
40
  "build": "tsc -p tsconfig.json; tsc -p tsconfig.esm.json",