@dxos/effect 0.6.12-main.568932b
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 +105 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +117 -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 +98 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/decamelize.d.ts +2 -0
- package/dist/types/src/decamelize.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -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 +42 -0
- package/src/decamelize.ts +36 -0
- package/src/index.ts +10 -0
- package/src/url.test.ts +38 -0
- package/src/url.ts +77 -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,105 @@
|
|
|
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
|
+
|
|
17
|
+
// packages/common/effect/src/decamelize.ts
|
|
18
|
+
var LOW_DASH = "_".codePointAt(0);
|
|
19
|
+
var SMALL_A = "a".codePointAt(0);
|
|
20
|
+
var CAPITAL_A = "A".codePointAt(0);
|
|
21
|
+
var SMALL_Z = "z".codePointAt(0);
|
|
22
|
+
var CAPITAL_Z = "Z".codePointAt(0);
|
|
23
|
+
var isLower = (char) => char >= SMALL_A && char <= SMALL_Z;
|
|
24
|
+
var isUpper = (char) => char >= CAPITAL_A && char <= CAPITAL_Z;
|
|
25
|
+
var toLower = (char) => char + 32;
|
|
26
|
+
var decamelize = (str) => {
|
|
27
|
+
const firstChar = str.charCodeAt(0);
|
|
28
|
+
if (!isLower(firstChar)) {
|
|
29
|
+
return str;
|
|
30
|
+
}
|
|
31
|
+
const length = str.length;
|
|
32
|
+
let changed = false;
|
|
33
|
+
const out = [];
|
|
34
|
+
for (let i = 0; i < length; ++i) {
|
|
35
|
+
const c = str.charCodeAt(i);
|
|
36
|
+
if (isUpper(c)) {
|
|
37
|
+
out.push(LOW_DASH);
|
|
38
|
+
out.push(toLower(c));
|
|
39
|
+
changed = true;
|
|
40
|
+
} else {
|
|
41
|
+
out.push(c);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return changed ? String.fromCharCode.apply(void 0, out) : str;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// packages/common/effect/src/url.ts
|
|
48
|
+
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
49
|
+
var getParamKeyAnnotation = AST.getAnnotation(ParamKeyAnnotationId);
|
|
50
|
+
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
51
|
+
[ParamKeyAnnotationId]: value
|
|
52
|
+
});
|
|
53
|
+
var UrlParser = class {
|
|
54
|
+
constructor(_schema) {
|
|
55
|
+
this._schema = _schema;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Parse URL params.
|
|
59
|
+
*/
|
|
60
|
+
parse(_url) {
|
|
61
|
+
const url = new URL(_url);
|
|
62
|
+
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
63
|
+
let value = url.searchParams.get(decamelize(key));
|
|
64
|
+
if (value == null) {
|
|
65
|
+
value = url.searchParams.get(key);
|
|
66
|
+
}
|
|
67
|
+
if (value != null) {
|
|
68
|
+
if (AST.isNumberKeyword(type.ast)) {
|
|
69
|
+
params[key] = parseInt(value);
|
|
70
|
+
} else if (AST.isBooleanKeyword(type.ast)) {
|
|
71
|
+
params[key] = value === "true" || value === "1";
|
|
72
|
+
} else {
|
|
73
|
+
params[key] = value;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return params;
|
|
77
|
+
}, {});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Return URL with encoded params.
|
|
81
|
+
*/
|
|
82
|
+
create(_url, params) {
|
|
83
|
+
const url = new URL(_url);
|
|
84
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
85
|
+
if (value !== void 0) {
|
|
86
|
+
const field = this._schema.fields[key];
|
|
87
|
+
if (field) {
|
|
88
|
+
const { key: serializedKey } = pipe(getParamKeyAnnotation(field.ast), Option.getOrElse(() => ({
|
|
89
|
+
key: decamelize(key)
|
|
90
|
+
})));
|
|
91
|
+
url.searchParams.set(serializedKey, String(value));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
return url;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export {
|
|
99
|
+
AST2 as AST,
|
|
100
|
+
JSONSchema,
|
|
101
|
+
ParamKeyAnnotation,
|
|
102
|
+
S,
|
|
103
|
+
UrlParser
|
|
104
|
+
};
|
|
105
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/url.ts", "../../../src/decamelize.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\nimport type * as Types from 'effect/Types';\n\nexport { AST, JSONSchema, S, Types };\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';\n\nimport { decamelize } from './decamelize';\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", "//\n// Copyright 2024 DXOS.org\n//\n\nconst LOW_DASH = '_'.codePointAt(0)!;\nconst SMALL_A = 'a'.codePointAt(0)!;\nconst CAPITAL_A = 'A'.codePointAt(0)!;\nconst SMALL_Z = 'z'.codePointAt(0)!;\nconst CAPITAL_Z = 'Z'.codePointAt(0)!;\n\nconst isLower = (char: number) => char >= SMALL_A && char <= SMALL_Z;\n\nconst isUpper = (char: number) => char >= CAPITAL_A && char <= CAPITAL_Z;\n\nconst toLower = (char: number) => char + 0x20;\n\nexport const decamelize = (str: string) => {\n const firstChar = str.charCodeAt(0);\n if (!isLower(firstChar)) {\n return str;\n }\n const length = str.length;\n let changed = false;\n const out: number[] = [];\n for (let i = 0; i < length; ++i) {\n const c = str.charCodeAt(i);\n if (isUpper(c)) {\n out.push(LOW_DASH);\n out.push(toLower(c));\n changed = true;\n } else {\n out.push(c);\n }\n }\n return changed ? String.fromCharCode.apply(undefined, out) : str;\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;;;ACD7B,IAAMC,WAAW,IAAIC,YAAY,CAAA;AACjC,IAAMC,UAAU,IAAID,YAAY,CAAA;AAChC,IAAME,YAAY,IAAIF,YAAY,CAAA;AAClC,IAAMG,UAAU,IAAIH,YAAY,CAAA;AAChC,IAAMI,YAAY,IAAIJ,YAAY,CAAA;AAElC,IAAMK,UAAU,CAACC,SAAiBA,QAAQL,WAAWK,QAAQH;AAE7D,IAAMI,UAAU,CAACD,SAAiBA,QAAQJ,aAAaI,QAAQF;AAE/D,IAAMI,UAAU,CAACF,SAAiBA,OAAO;AAElC,IAAMG,aAAa,CAACC,QAAAA;AACzB,QAAMC,YAAYD,IAAIE,WAAW,CAAA;AACjC,MAAI,CAACP,QAAQM,SAAAA,GAAY;AACvB,WAAOD;EACT;AACA,QAAMG,SAASH,IAAIG;AACnB,MAAIC,UAAU;AACd,QAAMC,MAAgB,CAAA;AACtB,WAASC,IAAI,GAAGA,IAAIH,QAAQ,EAAEG,GAAG;AAC/B,UAAMC,IAA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AAAA;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", "LOW_DASH", "codePointAt", "SMALL_A", "CAPITAL_A", "SMALL_Z", "CAPITAL_Z", "isLower", "char", "isUpper", "toLower", "decamelize", "str", "firstChar", "charCodeAt", "length", "changed", "out", "i", "c", "push", "String", "fromCharCode", "apply", "undefined", "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/decamelize.ts":{"bytes":3867,"imports":[{"path":"@inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7682,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"packages/common/effect/src/decamelize.ts","kind":"import-statement","original":"./decamelize"},{"path":"@inject-globals","kind":"import-statement","external":true}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":959,"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":6363},"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}],"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":1550},"packages/common/effect/src/decamelize.ts":{"bytesInOutput":798}},"bytes":2842}}}
|
|
@@ -0,0 +1,117 @@
|
|
|
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 LOW_DASH = "_".codePointAt(0);
|
|
32
|
+
var SMALL_A = "a".codePointAt(0);
|
|
33
|
+
var CAPITAL_A = "A".codePointAt(0);
|
|
34
|
+
var SMALL_Z = "z".codePointAt(0);
|
|
35
|
+
var CAPITAL_Z = "Z".codePointAt(0);
|
|
36
|
+
var isLower = (char) => char >= SMALL_A && char <= SMALL_Z;
|
|
37
|
+
var isUpper = (char) => char >= CAPITAL_A && char <= CAPITAL_Z;
|
|
38
|
+
var toLower = (char) => char + 32;
|
|
39
|
+
var decamelize = (str) => {
|
|
40
|
+
const firstChar = str.charCodeAt(0);
|
|
41
|
+
if (!isLower(firstChar)) {
|
|
42
|
+
return str;
|
|
43
|
+
}
|
|
44
|
+
const length = str.length;
|
|
45
|
+
let changed = false;
|
|
46
|
+
const out = [];
|
|
47
|
+
for (let i = 0; i < length; ++i) {
|
|
48
|
+
const c = str.charCodeAt(i);
|
|
49
|
+
if (isUpper(c)) {
|
|
50
|
+
out.push(LOW_DASH);
|
|
51
|
+
out.push(toLower(c));
|
|
52
|
+
changed = true;
|
|
53
|
+
} else {
|
|
54
|
+
out.push(c);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return changed ? String.fromCharCode.apply(void 0, out) : str;
|
|
58
|
+
};
|
|
59
|
+
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
60
|
+
var getParamKeyAnnotation = import_schema2.AST.getAnnotation(ParamKeyAnnotationId);
|
|
61
|
+
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
62
|
+
[ParamKeyAnnotationId]: value
|
|
63
|
+
});
|
|
64
|
+
var UrlParser = class {
|
|
65
|
+
constructor(_schema) {
|
|
66
|
+
this._schema = _schema;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Parse URL params.
|
|
70
|
+
*/
|
|
71
|
+
parse(_url) {
|
|
72
|
+
const url = new URL(_url);
|
|
73
|
+
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
74
|
+
let value = url.searchParams.get(decamelize(key));
|
|
75
|
+
if (value == null) {
|
|
76
|
+
value = url.searchParams.get(key);
|
|
77
|
+
}
|
|
78
|
+
if (value != null) {
|
|
79
|
+
if (import_schema2.AST.isNumberKeyword(type.ast)) {
|
|
80
|
+
params[key] = parseInt(value);
|
|
81
|
+
} else if (import_schema2.AST.isBooleanKeyword(type.ast)) {
|
|
82
|
+
params[key] = value === "true" || value === "1";
|
|
83
|
+
} else {
|
|
84
|
+
params[key] = value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return params;
|
|
88
|
+
}, {});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Return URL with encoded params.
|
|
92
|
+
*/
|
|
93
|
+
create(_url, params) {
|
|
94
|
+
const url = new URL(_url);
|
|
95
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
96
|
+
if (value !== void 0) {
|
|
97
|
+
const field = this._schema.fields[key];
|
|
98
|
+
if (field) {
|
|
99
|
+
const { key: serializedKey } = (0, import_effect.pipe)(getParamKeyAnnotation(field.ast), import_effect.Option.getOrElse(() => ({
|
|
100
|
+
key: decamelize(key)
|
|
101
|
+
})));
|
|
102
|
+
url.searchParams.set(serializedKey, String(value));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return url;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
110
|
+
0 && (module.exports = {
|
|
111
|
+
AST,
|
|
112
|
+
JSONSchema,
|
|
113
|
+
ParamKeyAnnotation,
|
|
114
|
+
S,
|
|
115
|
+
UrlParser
|
|
116
|
+
});
|
|
117
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/url.ts", "../../../src/decamelize.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\nimport type * as Types from 'effect/Types';\n\nexport { AST, JSONSchema, S, Types };\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';\n\nimport { decamelize } from './decamelize';\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", "//\n// Copyright 2024 DXOS.org\n//\n\nconst LOW_DASH = '_'.codePointAt(0)!;\nconst SMALL_A = 'a'.codePointAt(0)!;\nconst CAPITAL_A = 'A'.codePointAt(0)!;\nconst SMALL_Z = 'z'.codePointAt(0)!;\nconst CAPITAL_Z = 'Z'.codePointAt(0)!;\n\nconst isLower = (char: number) => char >= SMALL_A && char <= SMALL_Z;\n\nconst isUpper = (char: number) => char >= CAPITAL_A && char <= CAPITAL_Z;\n\nconst toLower = (char: number) => char + 0x20;\n\nexport const decamelize = (str: string) => {\n const firstChar = str.charCodeAt(0);\n if (!isLower(firstChar)) {\n return str;\n }\n const length = str.length;\n let changed = false;\n const out: number[] = [];\n for (let i = 0; i < length; ++i) {\n const c = str.charCodeAt(i);\n if (isUpper(c)) {\n out.push(LOW_DASH);\n out.push(toLower(c));\n changed = true;\n } else {\n out.push(c);\n }\n }\n return changed ? String.fromCharCode.apply(undefined, out) : str;\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,oBAA6C;ACA7C,IAAAA,iBAAsC;AACtC,oBAA6B;ACD7B,IAAMC,WAAW,IAAIC,YAAY,CAAA;A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4B,CAACC,QAAQ,CAACC,KAAKC,IAAAA,MAAK;AACzF,UAAIhB,QAAQQ,IAAIS,aAAaC,IAAIvC,WAAWoC,GAAAA,CAAAA;AAC5C,UAAIf,SAAS,MAAM;AACjBA,gBAAQQ,IAAIS,aAAaC,IAAIH,GAAAA;MAC/B;AAEA,UAAIf,SAAS,MAAM;AACjB,YAAIH,mBAAIsB,gBAAgBH,KAAKI,GAAG,GAAG;AACjCN,iBAAOC,GAAAA,IAAOM,SAASrB,KAAAA;QACzB,WAAWH,mBAAIyB,iBAAiBN,KAAKI,GAAG,GAAG;AACzCN,iBAAOC,GAAAA,IAAOf,UAAU,UAAUA,UAAU;QAC9C,OAAO;AACLc,iBAAOC,GAAAA,IAAOf;QAChB;MACF;AAEA,aAAOc;IACT,GAAG,CAAC,CAAA;EACN;;;;EAKAS,OAAOhB,MAAcO,QAAgB;AACnC,UAAMN,MAAM,IAAIC,IAAIF,IAAAA;AACpBG,WAAOC,QAAQG,MAAAA,EAAQU,QAAQ,CAAC,CAACT,KAAKf,KAAAA,MAAM;AAC1C,UAAIA,UAAUR,QAAW;AACvB,cAAMiC,QAAQ,KAAKpB,QAAQO,OAAOG,GAAAA;AAClC,YAAIU,OAAO;AACT,gBAAM,EAAEV,KAAKW,cAAa,QAAKC,oBAC7B/B,sBAAsB6B,MAAML,GAAG,GAC/BQ,qBAAOC,UAAU,OAAO;YACtBd,KAAKpC,WAAWoC,GAAAA;UAClB,EAAA,CAAA;AAGFP,cAAIS,aAAaa,IAAIJ,eAAerC,OAAOW,KAAAA,CAAAA;QAC7C;MACF;IACF,CAAA;AAEA,WAAOQ;EACT;AACF;",
|
|
6
|
+
"names": ["import_schema", "LOW_DASH", "codePointAt", "SMALL_A", "CAPITAL_A", "SMALL_Z", "CAPITAL_Z", "isLower", "char", "isUpper", "toLower", "decamelize", "str", "firstChar", "charCodeAt", "length", "changed", "out", "i", "c", "push", "String", "fromCharCode", "apply", "undefined", "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", "isNumberKeyword", "ast", "parseInt", "isBooleanKeyword", "create", "forEach", "field", "serializedKey", "pipe", "Option", "getOrElse", "set"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/common/effect/src/decamelize.ts":{"bytes":3867,"imports":[],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7682,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"packages/common/effect/src/decamelize.ts","kind":"import-statement","original":"./decamelize"}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":959,"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":6354},"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}],"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":1550},"packages/common/effect/src/decamelize.ts":{"bytesInOutput":798}},"bytes":2694}}}
|
|
@@ -0,0 +1,98 @@
|
|
|
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
|
+
|
|
10
|
+
// packages/common/effect/src/decamelize.ts
|
|
11
|
+
var LOW_DASH = "_".codePointAt(0);
|
|
12
|
+
var SMALL_A = "a".codePointAt(0);
|
|
13
|
+
var CAPITAL_A = "A".codePointAt(0);
|
|
14
|
+
var SMALL_Z = "z".codePointAt(0);
|
|
15
|
+
var CAPITAL_Z = "Z".codePointAt(0);
|
|
16
|
+
var isLower = (char) => char >= SMALL_A && char <= SMALL_Z;
|
|
17
|
+
var isUpper = (char) => char >= CAPITAL_A && char <= CAPITAL_Z;
|
|
18
|
+
var toLower = (char) => char + 32;
|
|
19
|
+
var decamelize = (str) => {
|
|
20
|
+
const firstChar = str.charCodeAt(0);
|
|
21
|
+
if (!isLower(firstChar)) {
|
|
22
|
+
return str;
|
|
23
|
+
}
|
|
24
|
+
const length = str.length;
|
|
25
|
+
let changed = false;
|
|
26
|
+
const out = [];
|
|
27
|
+
for (let i = 0; i < length; ++i) {
|
|
28
|
+
const c = str.charCodeAt(i);
|
|
29
|
+
if (isUpper(c)) {
|
|
30
|
+
out.push(LOW_DASH);
|
|
31
|
+
out.push(toLower(c));
|
|
32
|
+
changed = true;
|
|
33
|
+
} else {
|
|
34
|
+
out.push(c);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return changed ? String.fromCharCode.apply(void 0, out) : str;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// packages/common/effect/src/url.ts
|
|
41
|
+
var ParamKeyAnnotationId = Symbol.for("@dxos/schema/annotation/ParamKey");
|
|
42
|
+
var getParamKeyAnnotation = AST.getAnnotation(ParamKeyAnnotationId);
|
|
43
|
+
var ParamKeyAnnotation = (value) => (self) => self.annotations({
|
|
44
|
+
[ParamKeyAnnotationId]: value
|
|
45
|
+
});
|
|
46
|
+
var UrlParser = class {
|
|
47
|
+
constructor(_schema) {
|
|
48
|
+
this._schema = _schema;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Parse URL params.
|
|
52
|
+
*/
|
|
53
|
+
parse(_url) {
|
|
54
|
+
const url = new URL(_url);
|
|
55
|
+
return Object.entries(this._schema.fields).reduce((params, [key, type]) => {
|
|
56
|
+
let value = url.searchParams.get(decamelize(key));
|
|
57
|
+
if (value == null) {
|
|
58
|
+
value = url.searchParams.get(key);
|
|
59
|
+
}
|
|
60
|
+
if (value != null) {
|
|
61
|
+
if (AST.isNumberKeyword(type.ast)) {
|
|
62
|
+
params[key] = parseInt(value);
|
|
63
|
+
} else if (AST.isBooleanKeyword(type.ast)) {
|
|
64
|
+
params[key] = value === "true" || value === "1";
|
|
65
|
+
} else {
|
|
66
|
+
params[key] = value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return params;
|
|
70
|
+
}, {});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Return URL with encoded params.
|
|
74
|
+
*/
|
|
75
|
+
create(_url, params) {
|
|
76
|
+
const url = new URL(_url);
|
|
77
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
78
|
+
if (value !== void 0) {
|
|
79
|
+
const field = this._schema.fields[key];
|
|
80
|
+
if (field) {
|
|
81
|
+
const { key: serializedKey } = pipe(getParamKeyAnnotation(field.ast), Option.getOrElse(() => ({
|
|
82
|
+
key: decamelize(key)
|
|
83
|
+
})));
|
|
84
|
+
url.searchParams.set(serializedKey, String(value));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return url;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
export {
|
|
92
|
+
AST2 as AST,
|
|
93
|
+
JSONSchema,
|
|
94
|
+
ParamKeyAnnotation,
|
|
95
|
+
S,
|
|
96
|
+
UrlParser
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/url.ts", "../../../src/decamelize.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport { AST, JSONSchema, Schema as S } from '@effect/schema';\nimport type * as Types from 'effect/Types';\n\nexport { AST, JSONSchema, S, Types };\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';\n\nimport { decamelize } from './decamelize';\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", "//\n// Copyright 2024 DXOS.org\n//\n\nconst LOW_DASH = '_'.codePointAt(0)!;\nconst SMALL_A = 'a'.codePointAt(0)!;\nconst CAPITAL_A = 'A'.codePointAt(0)!;\nconst SMALL_Z = 'z'.codePointAt(0)!;\nconst CAPITAL_Z = 'Z'.codePointAt(0)!;\n\nconst isLower = (char: number) => char >= SMALL_A && char <= SMALL_Z;\n\nconst isUpper = (char: number) => char >= CAPITAL_A && char <= CAPITAL_Z;\n\nconst toLower = (char: number) => char + 0x20;\n\nexport const decamelize = (str: string) => {\n const firstChar = str.charCodeAt(0);\n if (!isLower(firstChar)) {\n return str;\n }\n const length = str.length;\n let changed = false;\n const out: number[] = [];\n for (let i = 0; i < length; ++i) {\n const c = str.charCodeAt(i);\n if (isUpper(c)) {\n out.push(LOW_DASH);\n out.push(toLower(c));\n changed = true;\n } else {\n out.push(c);\n }\n }\n return changed ? String.fromCharCode.apply(undefined, out) : str;\n};\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,OAAAA,MAAKC,YAAYC,UAAUC,SAAS;;;ACA7C,SAASC,WAA6B;AACtC,SAASC,QAAQC,YAAY;;;ACD7B,IAAMC,WAAW,IAAIC,YAAY,CAAA;AACjC,IAAMC,UAAU,IAAID,YAAY,CAAA;AAChC,IAAME,YAAY,IAAIF,YAAY,CAAA;AAClC,IAAMG,UAAU,IAAIH,YAAY,CAAA;AAChC,IAAMI,YAAY,IAAIJ,YAAY,CAAA;AAElC,IAAMK,UAAU,CAACC,SAAiBA,QAAQL,WAAWK,QAAQH;AAE7D,IAAMI,UAAU,CAACD,SAAiBA,QAAQJ,aAAaI,QAAQF;AAE/D,IAAMI,UAAU,CAACF,SAAiBA,OAAO;AAElC,IAAMG,aAAa,CAACC,QAAAA;AACzB,QAAMC,YAAYD,IAAIE,WAAW,CAAA;AACjC,MAAI,CAACP,QAAQM,SAAAA,GAAY;AACvB,WAAOD;EAC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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", "LOW_DASH", "codePointAt", "SMALL_A", "CAPITAL_A", "SMALL_Z", "CAPITAL_Z", "isLower", "char", "isUpper", "toLower", "decamelize", "str", "firstChar", "charCodeAt", "length", "changed", "out", "i", "c", "push", "String", "fromCharCode", "apply", "undefined", "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/decamelize.ts":{"bytes":3867,"imports":[],"format":"esm"},"packages/common/effect/src/url.ts":{"bytes":7682,"imports":[{"path":"@effect/schema","kind":"import-statement","external":true},{"path":"effect","kind":"import-statement","external":true},{"path":"packages/common/effect/src/decamelize.ts","kind":"import-statement","original":"./decamelize"}],"format":"esm"},"packages/common/effect/src/index.ts":{"bytes":959,"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":6356},"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}],"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":1550},"packages/common/effect/src/decamelize.ts":{"bytesInOutput":798}},"bytes":2787}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decamelize.d.ts","sourceRoot":"","sources":["../../../src/decamelize.ts"],"names":[],"mappings":"AAgBA,eAAO,MAAM,UAAU,QAAS,MAAM,WAmBrC,CAAC"}
|
|
@@ -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;AAC9D,OAAO,KAAK,KAAK,KAAK,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC;AAErC,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;AAOvD,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,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/effect",
|
|
3
|
+
"version": "0.6.12-main.568932b",
|
|
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
|
+
"@dxos/node-std": "0.6.12-main.568932b"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@effect/schema": "^0.75.1",
|
|
32
|
+
"effect": "^3.9.1"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@effect/schema": "^0.67.16",
|
|
36
|
+
"effect": "^3.2.7"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"beast": {}
|
|
42
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
const LOW_DASH = '_'.codePointAt(0)!;
|
|
6
|
+
const SMALL_A = 'a'.codePointAt(0)!;
|
|
7
|
+
const CAPITAL_A = 'A'.codePointAt(0)!;
|
|
8
|
+
const SMALL_Z = 'z'.codePointAt(0)!;
|
|
9
|
+
const CAPITAL_Z = 'Z'.codePointAt(0)!;
|
|
10
|
+
|
|
11
|
+
const isLower = (char: number) => char >= SMALL_A && char <= SMALL_Z;
|
|
12
|
+
|
|
13
|
+
const isUpper = (char: number) => char >= CAPITAL_A && char <= CAPITAL_Z;
|
|
14
|
+
|
|
15
|
+
const toLower = (char: number) => char + 0x20;
|
|
16
|
+
|
|
17
|
+
export const decamelize = (str: string) => {
|
|
18
|
+
const firstChar = str.charCodeAt(0);
|
|
19
|
+
if (!isLower(firstChar)) {
|
|
20
|
+
return str;
|
|
21
|
+
}
|
|
22
|
+
const length = str.length;
|
|
23
|
+
let changed = false;
|
|
24
|
+
const out: number[] = [];
|
|
25
|
+
for (let i = 0; i < length; ++i) {
|
|
26
|
+
const c = str.charCodeAt(i);
|
|
27
|
+
if (isUpper(c)) {
|
|
28
|
+
out.push(LOW_DASH);
|
|
29
|
+
out.push(toLower(c));
|
|
30
|
+
changed = true;
|
|
31
|
+
} else {
|
|
32
|
+
out.push(c);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return changed ? String.fromCharCode.apply(undefined, out) : str;
|
|
36
|
+
};
|
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,77 @@
|
|
|
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
|
+
|
|
8
|
+
import { decamelize } from './decamelize';
|
|
9
|
+
|
|
10
|
+
const ParamKeyAnnotationId = Symbol.for('@dxos/schema/annotation/ParamKey');
|
|
11
|
+
|
|
12
|
+
type ParamKeyAnnotationValue = { key: string };
|
|
13
|
+
|
|
14
|
+
const getParamKeyAnnotation: (annotated: AST.Annotated) => Option.Option<ParamKeyAnnotationValue> =
|
|
15
|
+
AST.getAnnotation<ParamKeyAnnotationValue>(ParamKeyAnnotationId);
|
|
16
|
+
|
|
17
|
+
export const ParamKeyAnnotation =
|
|
18
|
+
(value: ParamKeyAnnotationValue) =>
|
|
19
|
+
<S extends S.Annotable.All>(self: S): S.Annotable.Self<S> =>
|
|
20
|
+
self.annotations({ [ParamKeyAnnotationId]: value });
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* HTTP params parser.
|
|
24
|
+
* Supports custom key serialization.
|
|
25
|
+
*/
|
|
26
|
+
export class UrlParser<T extends Record<string, any>> {
|
|
27
|
+
constructor(private readonly _schema: S.Struct<T>) {}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Parse URL params.
|
|
31
|
+
*/
|
|
32
|
+
parse(_url: string): T {
|
|
33
|
+
const url = new URL(_url);
|
|
34
|
+
return Object.entries(this._schema.fields).reduce<Record<string, any>>((params, [key, type]) => {
|
|
35
|
+
let value = url.searchParams.get(decamelize(key));
|
|
36
|
+
if (value == null) {
|
|
37
|
+
value = url.searchParams.get(key);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (value != null) {
|
|
41
|
+
if (AST.isNumberKeyword(type.ast)) {
|
|
42
|
+
params[key] = parseInt(value);
|
|
43
|
+
} else if (AST.isBooleanKeyword(type.ast)) {
|
|
44
|
+
params[key] = value === 'true' || value === '1';
|
|
45
|
+
} else {
|
|
46
|
+
params[key] = value;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return params;
|
|
51
|
+
}, {}) as T;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Return URL with encoded params.
|
|
56
|
+
*/
|
|
57
|
+
create(_url: string, params: T): URL {
|
|
58
|
+
const url = new URL(_url);
|
|
59
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
60
|
+
if (value !== undefined) {
|
|
61
|
+
const field = this._schema.fields[key];
|
|
62
|
+
if (field) {
|
|
63
|
+
const { key: serializedKey } = pipe(
|
|
64
|
+
getParamKeyAnnotation(field.ast),
|
|
65
|
+
Option.getOrElse(() => ({
|
|
66
|
+
key: decamelize(key),
|
|
67
|
+
})),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
url.searchParams.set(serializedKey, String(value));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return url;
|
|
76
|
+
}
|
|
77
|
+
}
|