@aeriajs/common 0.0.144 → 0.0.146

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.
@@ -0,0 +1,2 @@
1
+ import type { Condition } from '@aeriajs/types';
2
+ export declare const convertConditionToQuery: (condition: Condition, subject?: Record<string, unknown>) => Record<string, unknown>;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertConditionToQuery = void 0;
4
+ const getValueFromPath_js_1 = require("./getValueFromPath.js");
5
+ const convertExpression = (condition, subject) => {
6
+ let term2 = null;
7
+ if ('term2' in condition) {
8
+ if (condition.fromState) {
9
+ term2 = subject && typeof condition.term2 === 'string'
10
+ ? (0, getValueFromPath_js_1.getValueFromPath)(subject, condition.term2)
11
+ : null;
12
+ }
13
+ else {
14
+ term2 = condition.term2;
15
+ }
16
+ }
17
+ switch (condition.operator) {
18
+ case 'truthy': return {
19
+ $ne: [
20
+ null,
21
+ undefined,
22
+ ],
23
+ };
24
+ case 'equal': return term2;
25
+ case 'gt': return {
26
+ $gt: term2,
27
+ };
28
+ case 'lt': return {
29
+ $lt: term2,
30
+ };
31
+ case 'gte': return {
32
+ $gte: term2,
33
+ };
34
+ case 'lte': return {
35
+ $lte: term2,
36
+ };
37
+ case 'regex': return {
38
+ $regex: term2,
39
+ $options: condition.regexOptions,
40
+ };
41
+ case 'in': {
42
+ return Array.isArray(term2)
43
+ ? {
44
+ $in: term2,
45
+ }
46
+ : term2;
47
+ }
48
+ }
49
+ };
50
+ const convertConditionToQuery = (condition, subject) => {
51
+ if ('or' in condition) {
52
+ return {
53
+ $or: condition.or.map((sub) => (0, exports.convertConditionToQuery)(sub, subject)),
54
+ };
55
+ }
56
+ if ('and' in condition) {
57
+ return {
58
+ $and: condition.and.map((sub) => (0, exports.convertConditionToQuery)(sub, subject)),
59
+ };
60
+ }
61
+ if ('not' in condition) {
62
+ return {
63
+ $not: (0, exports.convertConditionToQuery)(condition.not, subject),
64
+ };
65
+ }
66
+ return {
67
+ [condition.term1]: convertExpression(condition, subject),
68
+ };
69
+ };
70
+ exports.convertConditionToQuery = convertConditionToQuery;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ import { getValueFromPath } from "./getValueFromPath.mjs";
3
+ const convertExpression = (condition, subject) => {
4
+ let term2 = null;
5
+ if ("term2" in condition) {
6
+ if (condition.fromState) {
7
+ term2 = subject && typeof condition.term2 === "string" ? getValueFromPath(subject, condition.term2) : null;
8
+ } else {
9
+ term2 = condition.term2;
10
+ }
11
+ }
12
+ switch (condition.operator) {
13
+ case "truthy":
14
+ return {
15
+ $ne: [
16
+ null,
17
+ void 0
18
+ ]
19
+ };
20
+ case "equal":
21
+ return term2;
22
+ case "gt":
23
+ return {
24
+ $gt: term2
25
+ };
26
+ case "lt":
27
+ return {
28
+ $lt: term2
29
+ };
30
+ case "gte":
31
+ return {
32
+ $gte: term2
33
+ };
34
+ case "lte":
35
+ return {
36
+ $lte: term2
37
+ };
38
+ case "regex":
39
+ return {
40
+ $regex: term2,
41
+ $options: condition.regexOptions
42
+ };
43
+ case "in": {
44
+ return Array.isArray(term2) ? {
45
+ $in: term2
46
+ } : term2;
47
+ }
48
+ }
49
+ };
50
+ export const convertConditionToQuery = (condition, subject) => {
51
+ if ("or" in condition) {
52
+ return {
53
+ $or: condition.or.map((sub) => convertConditionToQuery(sub, subject))
54
+ };
55
+ }
56
+ if ("and" in condition) {
57
+ return {
58
+ $and: condition.and.map((sub) => convertConditionToQuery(sub, subject))
59
+ };
60
+ }
61
+ if ("not" in condition) {
62
+ return {
63
+ $not: convertConditionToQuery(condition.not, subject)
64
+ };
65
+ }
66
+ return {
67
+ [condition.term1]: convertExpression(condition, subject)
68
+ };
69
+ };
package/dist/http.d.ts CHANGED
@@ -3,21 +3,18 @@ export type RequestParams = Omit<RequestInit, 'headers'> & {
3
3
  };
4
4
  export type RequestConfig = {
5
5
  params?: RequestParams;
6
- requestTransformer?: (...args: Parameters<typeof defaultRequestTransformer>) => Promise<{
7
- url: string;
8
- params: RequestParams;
9
- }>;
10
- responseTransformer?: typeof defaultResponseTransformer;
6
+ requestTransformer?: RequestTransformer;
7
+ responseTransformer?: ResponseTransformer;
11
8
  };
12
- export declare const defaultRequestTransformer: (url: string, payload: unknown, params: RequestParams) => Promise<{
9
+ type OmitLastParameter<TFunction> = TFunction extends (...args: [...infer Tail, infer _Head]) => infer Return ? (...args: Tail) => Return : never;
10
+ export type RequestTransformer = (url: string, payload: unknown, params: RequestParams, next: (url: string, payload: unknown, params: RequestParams) => ReturnType<RequestTransformer>) => Promise<{
13
11
  url: string;
14
12
  params: RequestParams;
15
13
  }>;
16
- export declare const defaultResponseTransformer: (response: Awaited<ReturnType<typeof fetch>>) => Promise<Response & {
17
- data: unknown;
18
- }>;
14
+ export type ResponseTransformer = (response: Awaited<ReturnType<typeof fetch>>, next: (response: Awaited<ReturnType<typeof fetch>>) => ReturnType<ResponseTransformer>) => Promise<Awaited<ReturnType<typeof fetch>>>;
15
+ export declare const defaultRequestTransformer: OmitLastParameter<RequestTransformer>;
16
+ export declare const defaultResponseTransformer: OmitLastParameter<ResponseTransformer>;
19
17
  export declare const request: <TResponseType = unknown>(url: string, payload?: unknown, config?: RequestConfig) => Promise<Response & {
20
- data: unknown;
21
- } & {
22
18
  data: TResponseType;
23
19
  }>;
20
+ export {};
package/dist/http.js CHANGED
@@ -29,19 +29,29 @@ const defaultResponseTransformer = async (response) => {
29
29
  };
30
30
  exports.defaultResponseTransformer = defaultResponseTransformer;
31
31
  const request = async (url, payload, config = {}) => {
32
- const { requestTransformer = exports.defaultRequestTransformer, responseTransformer = exports.defaultResponseTransformer, params = {
33
- method: payload
34
- ? 'POST'
35
- : 'GET',
36
- headers: payload
37
- ? {
38
- 'content-type': 'application/json',
39
- }
40
- : {},
41
- }, } = config;
42
- const transformedRequest = await requestTransformer(url, payload, params);
32
+ const { requestTransformer = (url, payload, params, next) => next(url, payload, params), responseTransformer = (response, next) => next(response), } = config;
33
+ let params;
34
+ if (config.params) {
35
+ params = config.params;
36
+ }
37
+ else {
38
+ if (payload) {
39
+ params = {
40
+ method: 'POST',
41
+ headers: {
42
+ 'content-type': 'application/json',
43
+ },
44
+ };
45
+ }
46
+ else {
47
+ params = {
48
+ method: 'GET',
49
+ };
50
+ }
51
+ }
52
+ const transformedRequest = await requestTransformer(url, payload, params, exports.defaultRequestTransformer);
43
53
  const response = await fetch(transformedRequest.url, transformedRequest.params);
44
- const transformedResponse = await responseTransformer(response);
54
+ const transformedResponse = await responseTransformer(response, exports.defaultResponseTransformer);
45
55
  return transformedResponse;
46
56
  };
47
57
  exports.request = request;
package/dist/http.mjs CHANGED
@@ -23,17 +23,28 @@ export const defaultResponseTransformer = async (response) => {
23
23
  };
24
24
  export const request = async (url, payload, config = {}) => {
25
25
  const {
26
- requestTransformer = defaultRequestTransformer,
27
- responseTransformer = defaultResponseTransformer,
28
- params = {
29
- method: payload ? "POST" : "GET",
30
- headers: payload ? {
31
- "content-type": "application/json"
32
- } : {}
33
- }
26
+ requestTransformer = (url2, payload2, params2, next) => next(url2, payload2, params2),
27
+ responseTransformer = (response2, next) => next(response2)
34
28
  } = config;
35
- const transformedRequest = await requestTransformer(url, payload, params);
29
+ let params;
30
+ if (config.params) {
31
+ params = config.params;
32
+ } else {
33
+ if (payload) {
34
+ params = {
35
+ method: "POST",
36
+ headers: {
37
+ "content-type": "application/json"
38
+ }
39
+ };
40
+ } else {
41
+ params = {
42
+ method: "GET"
43
+ };
44
+ }
45
+ }
46
+ const transformedRequest = await requestTransformer(url, payload, params, defaultRequestTransformer);
36
47
  const response = await fetch(transformedRequest.url, transformedRequest.params);
37
- const transformedResponse = await responseTransformer(response);
48
+ const transformedResponse = await responseTransformer(response, defaultResponseTransformer);
38
49
  return transformedResponse;
39
50
  };
package/dist/index.d.ts CHANGED
@@ -16,5 +16,6 @@ export * from './isGranted.js';
16
16
  export * from './isReference.js';
17
17
  export * from './isRequired.js';
18
18
  export * from './isValidCollection.js';
19
+ export * from './convertConditionToQuery.js';
19
20
  export * from './pipe.js';
20
21
  export * from './serialize.js';
package/dist/index.js CHANGED
@@ -32,5 +32,6 @@ __exportStar(require("./isGranted.js"), exports);
32
32
  __exportStar(require("./isReference.js"), exports);
33
33
  __exportStar(require("./isRequired.js"), exports);
34
34
  __exportStar(require("./isValidCollection.js"), exports);
35
+ __exportStar(require("./convertConditionToQuery.js"), exports);
35
36
  __exportStar(require("./pipe.js"), exports);
36
37
  __exportStar(require("./serialize.js"), exports);
package/dist/index.mjs CHANGED
@@ -17,5 +17,6 @@ export * from "./isGranted.mjs";
17
17
  export * from "./isReference.mjs";
18
18
  export * from "./isRequired.mjs";
19
19
  export * from "./isValidCollection.mjs";
20
+ export * from "./convertConditionToQuery.mjs";
20
21
  export * from "./pipe.mjs";
21
22
  export * from "./serialize.mjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/common",
3
- "version": "0.0.144",
3
+ "version": "0.0.146",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -31,7 +31,7 @@
31
31
  "bson": "^6.5.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "@aeriajs/types": "^0.0.126",
34
+ "@aeriajs/types": "^0.0.127",
35
35
  "bson": "^6.5.0"
36
36
  },
37
37
  "scripts": {