@dxos/effect 0.6.12-main.89e9959
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 +8 -0
- package/README.md +15 -0
- package/dist/lib/browser/index.mjs +74 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +90 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +67 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/url.d.ts +23 -0
- package/dist/types/src/url.d.ts.map +1 -0
- package/dist/types/src/url.test.d.ts +2 -0
- package/dist/types/src/url.test.d.ts.map +1 -0
- package/package.json +43 -0
- package/src/index.ts +9 -0
- package/src/url.test.ts +38 -0
- package/src/url.ts +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2022 DXOS
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @dxos/effect
|
|
2
|
+
|
|
3
|
+
Effect utils.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm i @dxos/effect
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Contributions
|
|
12
|
+
|
|
13
|
+
Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
|
|
14
|
+
|
|
15
|
+
License: [MIT](./LICENSE) Copyright 2022 © DXOS
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import "@dxos/node-std/globals";
|
|
2
|
+
|
|
3
|
+
// inject-globals:@inject-globals
|
|
4
|
+
import {
|
|
5
|
+
global,
|
|
6
|
+
Buffer,
|
|
7
|
+
process
|
|
8
|
+
} from "@dxos/node-std/inject-globals";
|
|
9
|
+
|
|
10
|
+
// packages/common/effect/src/index.ts
|
|
11
|
+
import { AST as AST2, JSONSchema, Schema as S } from "@effect/schema";
|
|
12
|
+
|
|
13
|
+
// packages/common/effect/src/url.ts
|
|
14
|
+
import { AST } from "@effect/schema";
|
|
15
|
+
import { Option, pipe } from "effect";
|
|
16
|
+
import { decamelize } from "xcase";
|
|
17
|
+
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
18
|
+
var getParamKeyAnnotation = AST.getAnnotation(ParamKeyAnnotationId);
|
|
19
|
+
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
20
|
+
[ParamKeyAnnotationId]: value
|
|
21
|
+
});
|
|
22
|
+
var UrlParser = class {
|
|
23
|
+
constructor(_schema) {
|
|
24
|
+
this._schema = _schema;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parse URL params.
|
|
28
|
+
*/
|
|
29
|
+
parse(_url) {
|
|
30
|
+
const url = new URL(_url);
|
|
31
|
+
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
32
|
+
let value = url.searchParams.get(decamelize(key));
|
|
33
|
+
if (value == null) {
|
|
34
|
+
value = url.searchParams.get(key);
|
|
35
|
+
}
|
|
36
|
+
if (value != null) {
|
|
37
|
+
if (AST.isNumberKeyword(type.ast)) {
|
|
38
|
+
params[key] = parseInt(value);
|
|
39
|
+
} else if (AST.isBooleanKeyword(type.ast)) {
|
|
40
|
+
params[key] = value === "true" || value === "1";
|
|
41
|
+
} else {
|
|
42
|
+
params[key] = value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return params;
|
|
46
|
+
}, {});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Return URL with encoded params.
|
|
50
|
+
*/
|
|
51
|
+
create(_url, params) {
|
|
52
|
+
const url = new URL(_url);
|
|
53
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
54
|
+
if (value !== void 0) {
|
|
55
|
+
const field = this._schema.fields[key];
|
|
56
|
+
if (field) {
|
|
57
|
+
const { key: serializedKey } = pipe(getParamKeyAnnotation(field.ast), Option.getOrElse(() => ({
|
|
58
|
+
key: decamelize(key)
|
|
59
|
+
})));
|
|
60
|
+
url.searchParams.set(serializedKey, String(value));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return url;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
export {
|
|
68
|
+
AST2 as AST,
|
|
69
|
+
JSONSchema,
|
|
70
|
+
ParamKeyAnnotation,
|
|
71
|
+
S,
|
|
72
|
+
UrlParser
|
|
73
|
+
};
|
|
74
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/url.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\n\nexport { AST, JSONSchema, S };\n\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\nimport { decamelize } from 'xcase';\n\nconst ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');\n\ntype ParamKeyAnnotationValue = { key: string };\n\nconst getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =\n AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);\n\nexport const ParamKeyAnnotation =\n (value: ParamKeyAnnotationValue) =>\n <S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>\n self.annotations({ [ParamKeyAnnotationId]: value });\n\n/**\n * HTTP params parser.\n * Supports custom key serialization.\n */\nexport class UrlParser<T extends Record<string, any>> {\n constructor(private readonly _schema: S.Struct<T>) {}\n\n /**\n * Parse URL params.\n */\n parse(_url: string): T {\n const url = new URL(_url);\n return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {\n let value = url.searchParams.get(decamelize(key));\n if (value == null) {\n value = url.searchParams.get(key);\n }\n\n if (value != null) {\n if (AST.isNumberKeyword(type.ast)) {\n params[key] = parseInt(value);\n } else if (AST.isBooleanKeyword(type.ast)) {\n params[key] = value === 'true' || value === '1';\n } else {\n params[key] = value;\n }\n }\n\n return params;\n }, {}) as T;\n }\n\n /**\n * Return URL with encoded params.\n */\n create(_url: string, params: T): URL {\n const url = new URL(_url);\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined) {\n const field = this._schema.fields[key];\n if (field) {\n const { key: serializedKey } = pipe(\n getParamKeyAnnotation(field.ast),\n Option.getOrElse(() => ({\n key: decamelize(key),\n })),\n );\n\n url.searchParams.set(serializedKey, String(value));\n }\n }\n });\n\n return url;\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;AAC7B,SAASC,kBAAkB;AAE3B,IAAMC,uBAAuBC,OAAOC,IAAI,kCAAA;AAIxC,IAAMC,wBACJC,IAAIC,cAAuCL,oBAAAA;AAEtC,IAAMM,qBACX,CAACC,UACD,CAA4BC,SAC1BA,KAAKC,YAAY;EAAE,CAACT,oBAAAA,GAAuBO;AAAM,CAAA;AAM9C,IAAMG,YAAN,MAAMA;EACXC,YAA6BC,SAAsB;SAAtBA,UAAAA;EAAuB;;;;EAKpDC,MAAMC,MAAiB;AACrB,UAAMC,MAAM,IAAIC,IAAIF,IAAAA;AACpB,WAAOG,OAAOC,QAAQ,KAAKN,QAAQO,MAAM,EAAEC,OAA4B,CAACC,QAAQ,CAACC,KAAKC,IAAAA,MAAK;AACzF,UAAIhB,QAAQQ,IAAIS,aAAaC,IAAIC,WAAWJ,GAAAA,CAAAA;AAC5C,UAAIf,SAAS,MAAM;AACjBA,gBAAQQ,IAAIS,aAAaC,IAAIH,GAAAA;MAC/B;AAEA,UAAIf,SAAS,MAAM;AACjB,YAAIH,IAAIuB,gBAAgBJ,KAAKK,GAAG,GAAG;AACjCP,iBAAOC,GAAAA,IAAOO,SAAStB,KAAAA;QACzB,WAAWH,IAAI0B,iBAAiBP,KAAKK,GAAG,GAAG;AACzCP,iBAAOC,GAAAA,IAAOf,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACLc,iBAAOC,GAAAA,IAAOf;QAChB;MACF;AAEA,aAAOc;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAU,OAAOjB,MAAcO,QAAgB;AACnC,UAAMN,MAAM,IAAIC,IAAIF,IAAAA;AACpBG,WAAOC,QAAQG,MAAAA,EAAQW,QAAQ,CAAC,CAACV,KAAKf,KAAAA,MAAM;AAC1C,UAAIA,UAAU0B,QAAW;AACvB,cAAMC,QAAQ,KAAKtB,QAAQO,OAAOG,GAAAA;AAClC,YAAIY,OAAO;AACT,gBAAM,EAAEZ,KAAKa,cAAa,IAAKC,KAC7BjC,sBAAsB+B,MAAMN,GAAG,GAC/BS,OAAOC,UAAU,OAAO;YACtBhB,KAAKI,WAAWJ,GAAAA;UAClB,EAAA,CAAA;AAGFP,cAAIS,aAAae,IAAIJ,eAAeK,OAAOjC,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Option", "pipe", "decamelize", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "AST", "getAnnotation", "ParamKeyAnnotation", "value", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "entries", "fields", "reduce", "params", "key", "type", "searchParams", "get", "decamelize", "isNumberKeyword", "ast", "parseInt", "isBooleanKeyword", "create", "forEach", "undefined", "field", "serializedKey", "pipe", "Option", "getOrElse", "set", "String"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"inject-globals:@inject-globals":{"bytes":324,"imports":[{"path":"@dxos/node-std/inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7663,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"xcase","kind":"import-statement","external":true},{"path":"@inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":887,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"packages/common/effect/src/url.ts","kind":"import-statement","original":"./url"},{"path":"@inject-globals","kind":"import-statement","external":true}],"format":"esm"}},"outputs":{"packages/common/effect/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":4330},"packages/common/effect/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/node-std/inject-globals","kind":"import-statement","external":true},{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"xcase","kind":"import-statement","external":true}],"exports":["AST","JSONSchema","ParamKeyAnnotation","S","UrlParser"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"inject-globals:@inject-globals":{"bytesInOutput":79},"packages/common/effect/src/index.ts":{"bytesInOutput":71},"packages/common/effect/src/url.ts":{"bytesInOutput":1586}},"bytes":1997}}}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var node_exports = {};
|
|
20
|
+
__export(node_exports, {
|
|
21
|
+
AST: () => import_schema.AST,
|
|
22
|
+
JSONSchema: () => import_schema.JSONSchema,
|
|
23
|
+
ParamKeyAnnotation: () => ParamKeyAnnotation,
|
|
24
|
+
S: () => import_schema.Schema,
|
|
25
|
+
UrlParser: () => UrlParser
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(node_exports);
|
|
28
|
+
var import_schema = require("@effect/schema");
|
|
29
|
+
var import_schema2 = require("@effect/schema");
|
|
30
|
+
var import_effect = require("effect");
|
|
31
|
+
var import_xcase = require("xcase");
|
|
32
|
+
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
33
|
+
var getParamKeyAnnotation = import_schema2.AST.getAnnotation(ParamKeyAnnotationId);
|
|
34
|
+
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
35
|
+
[ParamKeyAnnotationId]: value
|
|
36
|
+
});
|
|
37
|
+
var UrlParser = class {
|
|
38
|
+
constructor(_schema) {
|
|
39
|
+
this._schema = _schema;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Parse URL params.
|
|
43
|
+
*/
|
|
44
|
+
parse(_url) {
|
|
45
|
+
const url = new URL(_url);
|
|
46
|
+
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
47
|
+
let value = url.searchParams.get((0, import_xcase.decamelize)(key));
|
|
48
|
+
if (value == null) {
|
|
49
|
+
value = url.searchParams.get(key);
|
|
50
|
+
}
|
|
51
|
+
if (value != null) {
|
|
52
|
+
if (import_schema2.AST.isNumberKeyword(type.ast)) {
|
|
53
|
+
params[key] = parseInt(value);
|
|
54
|
+
} else if (import_schema2.AST.isBooleanKeyword(type.ast)) {
|
|
55
|
+
params[key] = value === "true" || value === "1";
|
|
56
|
+
} else {
|
|
57
|
+
params[key] = value;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return params;
|
|
61
|
+
}, {});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Return URL with encoded params.
|
|
65
|
+
*/
|
|
66
|
+
create(_url, params) {
|
|
67
|
+
const url = new URL(_url);
|
|
68
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
69
|
+
if (value !== void 0) {
|
|
70
|
+
const field = this._schema.fields[key];
|
|
71
|
+
if (field) {
|
|
72
|
+
const { key: serializedKey } = (0, import_effect.pipe)(getParamKeyAnnotation(field.ast), import_effect.Option.getOrElse(() => ({
|
|
73
|
+
key: (0, import_xcase.decamelize)(key)
|
|
74
|
+
})));
|
|
75
|
+
url.searchParams.set(serializedKey, String(value));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return url;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
+
0 && (module.exports = {
|
|
84
|
+
AST,
|
|
85
|
+
JSONSchema,
|
|
86
|
+
ParamKeyAnnotation,
|
|
87
|
+
S,
|
|
88
|
+
UrlParser
|
|
89
|
+
});
|
|
90
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/url.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\n\nexport { AST, JSONSchema, S };\n\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\nimport { decamelize } from 'xcase';\n\nconst ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');\n\ntype ParamKeyAnnotationValue = { key: string };\n\nconst getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =\n AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);\n\nexport const ParamKeyAnnotation =\n (value: ParamKeyAnnotationValue) =>\n <S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>\n self.annotations({ [ParamKeyAnnotationId]: value });\n\n/**\n * HTTP params parser.\n * Supports custom key serialization.\n */\nexport class UrlParser<T extends Record<string, any>> {\n constructor(private readonly _schema: S.Struct<T>) {}\n\n /**\n * Parse URL params.\n */\n parse(_url: string): T {\n const url = new URL(_url);\n return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {\n let value = url.searchParams.get(decamelize(key));\n if (value == null) {\n value = url.searchParams.get(key);\n }\n\n if (value != null) {\n if (AST.isNumberKeyword(type.ast)) {\n params[key] = parseInt(value);\n } else if (AST.isBooleanKeyword(type.ast)) {\n params[key] = value === 'true' || value === '1';\n } else {\n params[key] = value;\n }\n }\n\n return params;\n }, {}) as T;\n }\n\n /**\n * Return URL with encoded params.\n */\n create(_url: string, params: T): URL {\n const url = new URL(_url);\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined) {\n const field = this._schema.fields[key];\n if (field) {\n const { key: serializedKey } = pipe(\n getParamKeyAnnotation(field.ast),\n Option.getOrElse(() => ({\n key: decamelize(key),\n })),\n );\n\n url.searchParams.set(serializedKey, String(value));\n }\n }\n });\n\n return url;\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,oBAA6C;ACA7C,IAAAA,iBAAsC;AACtC,oBAA6B;AAC7B,mBAA2B;AAE3B,IAAMC,uBAAuBC,OAAOC,IAAI,kCAAA;AAIxC,IAAMC,wBACJC,mBAAIC,cAAuCL,oBAAAA;AAEtC,IAAMM,qBACX,CAACC,UACD,CAA4BC,SAC1BA,KAAKC,YAAY;EAAE,CAACT,oBAAAA,GAAuBO;AAAM,CAAA;AAM9C,IAAMG,YAAN,MAAMA;EACXC,YAA6BC,SAAsB;SAAtBA,UAAAA;EAAuB;;;;EAKpDC,MAAMC,MAAiB;AACrB,UAAMC,MAAM,IAAIC,IAAIF,IAAAA;AACpB,WAAOG,OAAOC,QAAQ,KAAKN,QAAQO,MAAM,EAAEC,OAA4B,CAACC,QAAQ,CAACC,KAAKC,IAAAA,MAAK;AACzF,UAAIhB,QAAQQ,IAAIS,aAAaC,QAAIC,yBAAWJ,GAAAA,CAAAA;AAC5C,UAAIf,SAAS,MAAM;AACjBA,gBAAQQ,IAAIS,aAAaC,IAAIH,GAAAA;MAC/B;AAEA,UAAIf,SAAS,MAAM;AACjB,YAAIH,mBAAIuB,gBAAgBJ,KAAKK,GAAG,GAAG;AACjCP,iBAAOC,GAAAA,IAAOO,SAAStB,KAAAA;QACzB,WAAWH,mBAAI0B,iBAAiBP,KAAKK,GAAG,GAAG;AACzCP,iBAAOC,GAAAA,IAAOf,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACLc,iBAAOC,GAAAA,IAAOf;QAChB;MACF;AAEA,aAAOc;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAU,OAAOjB,MAAcO,QAAgB;AACnC,UAAMN,MAAM,IAAIC,IAAIF,IAAAA;AACpBG,WAAOC,QAAQG,MAAAA,EAAQW,QAAQ,CAAC,CAACV,KAAKf,KAAAA,MAAM;AAC1C,UAAIA,UAAU0B,QAAW;AACvB,cAAMC,QAAQ,KAAKtB,QAAQO,OAAOG,GAAAA;AAClC,YAAIY,OAAO;AACT,gBAAM,EAAEZ,KAAKa,cAAa,QAAKC,oBAC7BjC,sBAAsB+B,MAAMN,GAAG,GAC/BS,qBAAOC,UAAU,OAAO;YACtBhB,SAAKI,yBAAWJ,GAAAA;UAClB,EAAA,CAAA;AAGFP,cAAIS,aAAae,IAAIJ,eAAeK,OAAOjC,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["import_schema", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "AST", "getAnnotation", "ParamKeyAnnotation", "value", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "entries", "fields", "reduce", "params", "key", "type", "searchParams", "get", "decamelize", "isNumberKeyword", "ast", "parseInt", "isBooleanKeyword", "create", "forEach", "undefined", "field", "serializedKey", "pipe", "Option", "getOrElse", "set", "String"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/common/effect/src/url.ts":{"bytes":7663,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"xcase","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":887,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"packages/common/effect/src/url.ts","kind":"import-statement","original":"./url"}],"format":"esm"}},"outputs":{"packages/common/effect/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":4321},"packages/common/effect/dist/lib/node/index.cjs":{"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"xcase","kind":"import-statement","external":true}],"exports":["AST","JSONSchema","ParamKeyAnnotation","S","UrlParser"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":71},"packages/common/effect/src/url.ts":{"bytesInOutput":1586}},"bytes":1849}}}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// packages/common/effect/src/index.ts
|
|
4
|
+
import { AST as AST2, JSONSchema, Schema as S } from "@effect/schema";
|
|
5
|
+
|
|
6
|
+
// packages/common/effect/src/url.ts
|
|
7
|
+
import { AST } from "@effect/schema";
|
|
8
|
+
import { Option, pipe } from "effect";
|
|
9
|
+
import { decamelize } from "xcase";
|
|
10
|
+
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
11
|
+
var getParamKeyAnnotation = AST.getAnnotation(ParamKeyAnnotationId);
|
|
12
|
+
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
13
|
+
[ParamKeyAnnotationId]: value
|
|
14
|
+
});
|
|
15
|
+
var UrlParser = class {
|
|
16
|
+
constructor(_schema) {
|
|
17
|
+
this._schema = _schema;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Parse URL params.
|
|
21
|
+
*/
|
|
22
|
+
parse(_url) {
|
|
23
|
+
const url = new URL(_url);
|
|
24
|
+
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
25
|
+
let value = url.searchParams.get(decamelize(key));
|
|
26
|
+
if (value == null) {
|
|
27
|
+
value = url.searchParams.get(key);
|
|
28
|
+
}
|
|
29
|
+
if (value != null) {
|
|
30
|
+
if (AST.isNumberKeyword(type.ast)) {
|
|
31
|
+
params[key] = parseInt(value);
|
|
32
|
+
} else if (AST.isBooleanKeyword(type.ast)) {
|
|
33
|
+
params[key] = value === "true" || value === "1";
|
|
34
|
+
} else {
|
|
35
|
+
params[key] = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return params;
|
|
39
|
+
}, {});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Return URL with encoded params.
|
|
43
|
+
*/
|
|
44
|
+
create(_url, params) {
|
|
45
|
+
const url = new URL(_url);
|
|
46
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
47
|
+
if (value !== void 0) {
|
|
48
|
+
const field = this._schema.fields[key];
|
|
49
|
+
if (field) {
|
|
50
|
+
const { key: serializedKey } = pipe(getParamKeyAnnotation(field.ast), Option.getOrElse(() => ({
|
|
51
|
+
key: decamelize(key)
|
|
52
|
+
})));
|
|
53
|
+
url.searchParams.set(serializedKey, String(value));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return url;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
export {
|
|
61
|
+
AST2 as AST,
|
|
62
|
+
JSONSchema,
|
|
63
|
+
ParamKeyAnnotation,
|
|
64
|
+
S,
|
|
65
|
+
UrlParser
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/url.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\n\nexport { AST, JSONSchema, S };\n\nexport * from './url';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AST, type Schema as S } from '@effect/schema';\nimport { Option, pipe } from 'effect';\nimport { decamelize } from 'xcase';\n\nconst ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');\n\ntype ParamKeyAnnotationValue = { key: string };\n\nconst getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =\n AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);\n\nexport const ParamKeyAnnotation =\n (value: ParamKeyAnnotationValue) =>\n <S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>\n self.annotations({ [ParamKeyAnnotationId]: value });\n\n/**\n * HTTP params parser.\n * Supports custom key serialization.\n */\nexport class UrlParser<T extends Record<string, any>> {\n constructor(private readonly _schema: S.Struct<T>) {}\n\n /**\n * Parse URL params.\n */\n parse(_url: string): T {\n const url = new URL(_url);\n return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {\n let value = url.searchParams.get(decamelize(key));\n if (value == null) {\n value = url.searchParams.get(key);\n }\n\n if (value != null) {\n if (AST.isNumberKeyword(type.ast)) {\n params[key] = parseInt(value);\n } else if (AST.isBooleanKeyword(type.ast)) {\n params[key] = value === 'true' || value === '1';\n } else {\n params[key] = value;\n }\n }\n\n return params;\n }, {}) as T;\n }\n\n /**\n * Return URL with encoded params.\n */\n create(_url: string, params: T): URL {\n const url = new URL(_url);\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined) {\n const field = this._schema.fields[key];\n if (field) {\n const { key: serializedKey } = pipe(\n getParamKeyAnnotation(field.ast),\n Option.getOrElse(() => ({\n key: decamelize(key),\n })),\n );\n\n url.searchParams.set(serializedKey, String(value));\n }\n }\n });\n\n return url;\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;AAC7B,SAASC,kBAAkB;AAE3B,IAAMC,uBAAuBC,OAAOC,IAAI,kCAAA;AAIxC,IAAMC,wBACJC,IAAIC,cAAuCL,oBAAAA;AAEtC,IAAMM,qBACX,CAACC,UACD,CAA4BC,SAC1BA,KAAKC,YAAY;EAAE,CAACT,oBAAAA,GAAuBO;AAAM,CAAA;AAM9C,IAAMG,YAAN,MAAMA;EACXC,YAA6BC,SAAsB;SAAtBA,UAAAA;EAAuB;;;;EAKpDC,MAAMC,MAAiB;AACrB,UAAMC,MAAM,IAAIC,IAAIF,IAAAA;AACpB,WAAOG,OAAOC,QAAQ,KAAKN,QAAQO,MAAM,EAAEC,OAA4B,CAACC,QAAQ,CAACC,KAAKC,IAAAA,MAAK;AACzF,UAAIhB,QAAQQ,IAAIS,aAAaC,IAAIC,WAAWJ,GAAAA,CAAAA;AAC5C,UAAIf,SAAS,MAAM;AACjBA,gBAAQQ,IAAIS,aAAaC,IAAIH,GAAAA;MAC/B;AAEA,UAAIf,SAAS,MAAM;AACjB,YAAIH,IAAIuB,gBAAgBJ,KAAKK,GAAG,GAAG;AACjCP,iBAAOC,GAAAA,IAAOO,SAAStB,KAAAA;QACzB,WAAWH,IAAI0B,iBAAiBP,KAAKK,GAAG,GAAG;AACzCP,iBAAOC,GAAAA,IAAOf,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACLc,iBAAOC,GAAAA,IAAOf;QAChB;MACF;AAEA,aAAOc;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAU,OAAOjB,MAAcO,QAAgB;AACnC,UAAMN,MAAM,IAAIC,IAAIF,IAAAA;AACpBG,WAAOC,QAAQG,MAAAA,EAAQW,QAAQ,CAAC,CAACV,KAAKf,KAAAA,MAAM;AAC1C,UAAIA,UAAU0B,QAAW;AACvB,cAAMC,QAAQ,KAAKtB,QAAQO,OAAOG,GAAAA;AAClC,YAAIY,OAAO;AACT,gBAAM,EAAEZ,KAAKa,cAAa,IAAKC,KAC7BjC,sBAAsB+B,MAAMN,GAAG,GAC/BS,OAAOC,UAAU,OAAO;YACtBhB,KAAKI,WAAWJ,GAAAA;UAClB,EAAA,CAAA;AAGFP,cAAIS,aAAae,IAAIJ,eAAeK,OAAOjC,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["AST", "JSONSchema", "Schema", "S", "AST", "Option", "pipe", "decamelize", "ParamKeyAnnotationId", "Symbol", "for", "getParamKeyAnnotation", "AST", "getAnnotation", "ParamKeyAnnotation", "value", "self", "annotations", "UrlParser", "constructor", "_schema", "parse", "_url", "url", "URL", "Object", "entries", "fields", "reduce", "params", "key", "type", "searchParams", "get", "decamelize", "isNumberKeyword", "ast", "parseInt", "isBooleanKeyword", "create", "forEach", "undefined", "field", "serializedKey", "pipe", "Option", "getOrElse", "set", "String"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/common/effect/src/url.ts":{"bytes":7663,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"xcase","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":887,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"packages/common/effect/src/url.ts","kind":"import-statement","original":"./url"}],"format":"esm"}},"outputs":{"packages/common/effect/dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":4323},"packages/common/effect/dist/lib/node-esm/index.mjs":{"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"xcase","kind":"import-statement","external":true}],"exports":["AST","JSONSchema","ParamKeyAnnotation","S","UrlParser"],"entryPoint":"packages/common/effect/src/index.ts","inputs":{"packages/common/effect/src/index.ts":{"bytesInOutput":71},"packages/common/effect/src/url.ts":{"bytesInOutput":1586}},"bytes":1942}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC,EAAE,MAAM,gBAAgB,CAAC;AAE9D,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;AAE9B,cAAc,OAAO,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Schema as S } from '@effect/schema';
|
|
2
|
+
type ParamKeyAnnotationValue = {
|
|
3
|
+
key: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const ParamKeyAnnotation: (value: ParamKeyAnnotationValue) => <S extends S.Annotable.All>(self: S) => S.Annotable.Self<S>;
|
|
6
|
+
/**
|
|
7
|
+
* HTTP params parser.
|
|
8
|
+
* Supports custom key serialization.
|
|
9
|
+
*/
|
|
10
|
+
export declare class UrlParser<T extends Record<string, any>> {
|
|
11
|
+
private readonly _schema;
|
|
12
|
+
constructor(_schema: S.Struct<T>);
|
|
13
|
+
/**
|
|
14
|
+
* Parse URL params.
|
|
15
|
+
*/
|
|
16
|
+
parse(_url: string): T;
|
|
17
|
+
/**
|
|
18
|
+
* Return URL with encoded params.
|
|
19
|
+
*/
|
|
20
|
+
create(_url: string, params: T): URL;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=url.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../../src/url.ts"],"names":[],"mappings":"AAIA,OAAO,EAAO,KAAK,MAAM,IAAI,CAAC,EAAE,MAAM,gBAAgB,CAAC;AAMvD,KAAK,uBAAuB,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAK/C,eAAO,MAAM,kBAAkB,UACrB,uBAAuB,MAC9B,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,GAAG,QAAQ,CAAC,KAAG,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CACH,CAAC;AAExD;;;GAGG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IACtC,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IAEjD;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC;IAsBtB;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,GAAG;CAoBrC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"url.test.d.ts","sourceRoot":"","sources":["../../../src/url.test.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/effect",
|
|
3
|
+
"version": "0.6.12-main.89e9959",
|
|
4
|
+
"description": "Effect utils.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "info@dxos.org",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
12
|
+
"node": {
|
|
13
|
+
"require": "./dist/lib/node/index.cjs",
|
|
14
|
+
"default": "./dist/lib/node-esm/index.mjs"
|
|
15
|
+
},
|
|
16
|
+
"types": "./dist/types/src/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "dist/types/src/index.d.ts",
|
|
20
|
+
"typesVersions": {
|
|
21
|
+
"*": {}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"src"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"xcase": "^2.0.1",
|
|
29
|
+
"@dxos/node-std": "0.6.12-main.89e9959"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@effect/schema": "^0.67.16",
|
|
33
|
+
"effect": "^3.2.7"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@effect/schema": "^0.67.16",
|
|
37
|
+
"effect": "^3.2.7"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"beast": {}
|
|
43
|
+
}
|
package/src/index.ts
ADDED
package/src/url.test.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema as S } from '@effect/schema';
|
|
6
|
+
import { describe, expect, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { ParamKeyAnnotation, UrlParser } from './url';
|
|
9
|
+
|
|
10
|
+
const Invitation = S.Struct({
|
|
11
|
+
accessToken: S.String,
|
|
12
|
+
deviceInvitationCode: S.String.pipe(ParamKeyAnnotation({ key: 'deviceInvitationCode' })),
|
|
13
|
+
spaceInvitationCode: S.String,
|
|
14
|
+
experimental: S.Boolean,
|
|
15
|
+
testing: S.Boolean,
|
|
16
|
+
timeout: S.Number,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('Params', () => {
|
|
20
|
+
test('parse', () => {
|
|
21
|
+
const parser = new UrlParser(Invitation);
|
|
22
|
+
const values = parser.parse(
|
|
23
|
+
'http://localhost?access_token=100&deviceInvitationCode=200&experimental=1&testing=false&timeout=100',
|
|
24
|
+
);
|
|
25
|
+
expect(values).to.deep.eq({
|
|
26
|
+
accessToken: '100',
|
|
27
|
+
deviceInvitationCode: '200',
|
|
28
|
+
experimental: true,
|
|
29
|
+
testing: false,
|
|
30
|
+
timeout: 100,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const url = parser.create('http://localhost', values);
|
|
34
|
+
expect(url.toString()).to.eq(
|
|
35
|
+
'http://localhost/?access_token=100&deviceInvitationCode=200&experimental=true&testing=false&timeout=100',
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
});
|
package/src/url.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { AST, type Schema as S } from '@effect/schema';
|
|
6
|
+
import { Option, pipe } from 'effect';
|
|
7
|
+
import { decamelize } from 'xcase';
|
|
8
|
+
|
|
9
|
+
const ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');
|
|
10
|
+
|
|
11
|
+
type ParamKeyAnnotationValue = { key: string };
|
|
12
|
+
|
|
13
|
+
const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =
|
|
14
|
+
AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);
|
|
15
|
+
|
|
16
|
+
export const ParamKeyAnnotation =
|
|
17
|
+
(value: ParamKeyAnnotationValue) =>
|
|
18
|
+
<S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>
|
|
19
|
+
self.annotations({ [ParamKeyAnnotationId]: value });
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* HTTP params parser.
|
|
23
|
+
* Supports custom key serialization.
|
|
24
|
+
*/
|
|
25
|
+
export class UrlParser<T extends Record<string, any>> {
|
|
26
|
+
constructor(private readonly _schema: S.Struct<T>) {}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Parse URL params.
|
|
30
|
+
*/
|
|
31
|
+
parse(_url: string): T {
|
|
32
|
+
const url = new URL(_url);
|
|
33
|
+
return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {
|
|
34
|
+
let value = url.searchParams.get(decamelize(key));
|
|
35
|
+
if (value == null) {
|
|
36
|
+
value = url.searchParams.get(key);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (value != null) {
|
|
40
|
+
if (AST.isNumberKeyword(type.ast)) {
|
|
41
|
+
params[key] = parseInt(value);
|
|
42
|
+
} else if (AST.isBooleanKeyword(type.ast)) {
|
|
43
|
+
params[key] = value === 'true' || value === '1';
|
|
44
|
+
} else {
|
|
45
|
+
params[key] = value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return params;
|
|
50
|
+
}, {}) as T;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Return URL with encoded params.
|
|
55
|
+
*/
|
|
56
|
+
create(_url: string, params: T): URL {
|
|
57
|
+
const url = new URL(_url);
|
|
58
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
59
|
+
if (value !== undefined) {
|
|
60
|
+
const field = this._schema.fields[key];
|
|
61
|
+
if (field) {
|
|
62
|
+
const { key: serializedKey } = pipe(
|
|
63
|
+
getParamKeyAnnotation(field.ast),
|
|
64
|
+
Option.getOrElse(() => ({
|
|
65
|
+
key: decamelize(key),
|
|
66
|
+
})),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
url.searchParams.set(serializedKey, String(value));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return url;
|
|
75
|
+
}
|
|
76
|
+
}
|