@buildinams/core 0.3.0 → 0.3.1
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/dist/{chunk-IAAQVL6M.js → chunk-5KDGGGVO.js} +3 -2
- package/dist/chunk-5KDGGGVO.js.map +1 -0
- package/dist/navigation/index.d.ts +1 -1
- package/dist/navigation/index.js +1 -1
- package/dist/schema/index.d.ts +14 -9
- package/dist/schema/index.js +1 -1
- package/dist/schema-CfQvhbuU.d.ts +87 -0
- package/package.json +2 -2
- package/dist/chunk-IAAQVL6M.js.map +0 -1
- package/dist/schema-iy92gbTC.d.ts +0 -36
|
@@ -29,11 +29,12 @@ var hrefSchema = createStringSchema({
|
|
|
29
29
|
};
|
|
30
30
|
},
|
|
31
31
|
stringify: (state) => {
|
|
32
|
+
if (!state.searchParams) return state.pathname;
|
|
32
33
|
const paramsString = state.searchParams.toString();
|
|
33
34
|
return state.pathname + (paramsString ? `?${paramsString}` : "");
|
|
34
35
|
}
|
|
35
36
|
});
|
|
36
37
|
|
|
37
38
|
export { createSchema, createStringSchema, hrefSchema };
|
|
38
|
-
//# sourceMappingURL=chunk-
|
|
39
|
-
//# sourceMappingURL=chunk-
|
|
39
|
+
//# sourceMappingURL=chunk-5KDGGGVO.js.map
|
|
40
|
+
//# sourceMappingURL=chunk-5KDGGGVO.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schema/schema.ts","../src/schema/string-schema.ts","../src/schema/href-schema.ts"],"names":[],"mappings":";AAkGO,IAAM,YAAA,GAAe,CAK3B,QAAA,MACmD;AAAA,EACnD,GAAG,QAAA;AAAA,EAEH,YAAA,EAAc,CAAC,KAAA,EAAO,IAAA,KAAS;AAC9B,IAAA,OAAO,SAAS,SAAA,CAAU,IAAA,CAAK,SAAS,KAAA,CAAM,KAAK,CAAC,CAAC,CAAA;AAAA,EACtD;AACD,CAAA;;;AC9FO,IAAM,kBAAA,GAAqB,CACjC,QAAA,KAC4C;AAC5C,EAAA,MAAM,SAAS,YAAA,CAA+C;AAAA,IAC7D,WAAW,QAAA,CAAS,SAAA;AAAA,IACpB,OAAO,QAAA,CAAS;AAAA,GAChB,CAAA;AAED,EAAA,OAAO;AAAA,IACN,OAAO,MAAA,CAAO,KAAA;AAAA,IACd,WAAW,MAAA,CAAO,SAAA;AAAA,IAClB,gBAAgB,MAAA,CAAO;AAAA,GACxB;AACD;;;ACKO,IAAM,aAAa,kBAAA,CASxB;AAAA,EACD,KAAA,EAAO,CAAC,IAAA,KAAS;AAChB,IAAA,MAAM,CAAC,WAAW,EAAA,EAAI,MAAA,GAAS,EAAE,CAAA,GAAI,IAAA,CAAK,MAAM,GAAG,CAAA;AACnD,IAAA,OAAO;AAAA,MACN,QAAA;AAAA,MACA,YAAA,EAAc,IAAI,eAAA,CAAgB,MAAM;AAAA,KACzC;AAAA,EACD,CAAA;AAAA,EACA,SAAA,EAAW,CAAC,KAAA,KAAU;AACrB,IAAA,IAAI,CAAC,KAAA,CAAM,YAAA,EAAc,OAAO,KAAA,CAAM,QAAA;AACtC,IAAA,MAAM,YAAA,GAAe,KAAA,CAAM,YAAA,CAAa,QAAA,EAAS;AACjD,IAAA,OAAO,KAAA,CAAM,QAAA,IAAY,YAAA,GAAe,CAAA,CAAA,EAAI,YAAY,CAAA,CAAA,GAAK,EAAA,CAAA;AAAA,EAC9D;AACD,CAAC","file":"chunk-5KDGGGVO.js","sourcesContent":["/**\n * Defines the schema for parsing and stringifying states to and from primitive values.\n *\n * The `Schema` type is a generic type that takes:\n * - `State`: A state object type.\n * - `Serialized`: The raw value type\n * - `SerializableState?`: (optional) A (partial) state object type that can be used for serialization. Defaults to `State` if not provided.\n *\n * It returns the following methods:\n * - `parse`: A function that takes a raw value and returns the corresponding state object.\n * - `serialize`: A function that takes a (serializable) state object and returns a raw value that represents the given state.\n * - `parserialize`: A function that takes a raw value and a reducer function - it returns a new raw value that represents the reduced state.\n *\n * @see createSchema for a helper function to create a `Schema` from simpler parsing and serializing functions.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Schema<State = any, Serialized = any, SerializableState = State> = {\n\t/** Parse a raw value into the state shape */\n\tparse: (value: Serialized) => State;\n\t/** Serialize a state shape into a raw value */\n\tserialize: (state: SerializableState) => Serialized;\n\n\t/** Compute a new serialized value based on a given serialized value and a reducer of the current state. */\n\tparserialize: (\n\t\tvalue: Serialized,\n\t\tnext: (state: State) => SerializableState,\n\t) => Serialized;\n};\n\n/**\n * A utility type to extract the state shape from a given `Schema`.\n */\nexport type SchemaState<T extends Schema> =\n\tT extends Schema<infer U> ? U : never;\n\n/**\n * A utility type to extract the serialized value type from a given `Schema`.\n */\nexport type SchemaSerialized<T extends Schema> =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tT extends Schema<any, infer U> ? U : never;\n\n/**\n * A utility type to extract the serializable state shape from a given `Schema`.\n */\nexport type SchemaSerializableState<T extends Schema> =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tT extends Schema<any, any, infer U> ? U : never;\n\n/**\n * Low-level helper function to create a basic `Schema` that receives the config for converting between serialized values and state objects.\n *\n * The template requires two methods:\n * - `parse`: A method that takes a serialized value and returns the corresponding state object.\n * - `serialize`: A method that takes a state object and returns a serialized value that represents the given state.\n *\n * The `parserialize` method is automatically implemented based on the provided `parse` and `serialize` methods, allowing you to compute a new serialized value based on an existing serialized value and a reducer function for the state.\n *\n * @example\n * ```ts\n * import { createSchema } from \"@buildinams/core/schema\";\n *\n * const numberSchema = createSchema<\n * number, // State\n * string // Serialized\n * >({\n * parse: (value) => parseInt(value, 10),\n * serialize: (state) => state.toString(),\n * });\n *\n * const result = numberSchema.parserialize(\"42\", (state) => state + 1);\n * console.log(result); // \"43\"\n * ```\n *\n * @example\n * ```ts\n * import { createSchema } from \"@buildinams/core/schema\";\n *\n * const hrefSchema: createSchema<\n * { pathname: string, searchParams: URLSearchParams },\n * string,\n * { pathname: string, searchParams?: URLSearchParams } // SerializableState allows to define different state used for serialization, in this case making searchParams optional to allow for more flexible stringification logic\n * >({\n * parse: (href) => {\n * const [pathname = \"\", search = \"\"] = href.split(\"?\");\n * return {\n * pathname,\n * searchParams: new URLSearchParams(search),\n * };\n * },\n * serialize: (serializableState) => {\n * if (!serializableState.searchParams) return serializableState.pathname;\n * const paramsString = serializableState.searchParams.toString();\n * return serializableState.pathname + (paramsString ? `?${paramsString}` : \"\");\n * },\n * })\n * ```\n */\nexport const createSchema = <\n\tState = never,\n\tSerialized = never,\n\tSerializableState = State,\n>(\n\ttemplate: Omit<Schema<State, Serialized, SerializableState>, \"parserialize\">,\n): Schema<State, Serialized, SerializableState> => ({\n\t...template,\n\n\tparserialize: (value, next) => {\n\t\treturn template.serialize(next(template.parse(value)));\n\t},\n});\n","import type { Schema } from \"./schema\";\nimport { createSchema } from \"./schema\";\n\nexport type StringSchema<State = never, SerializableState = State> = {\n\tparse: Schema<State, string, SerializableState>[\"parse\"];\n\tstringify: Schema<State, string, SerializableState>[\"serialize\"];\n\tparseStringify: Schema<State, string, SerializableState>[\"parserialize\"];\n};\n\n/**\n * Helper function to create a `StringSchema` which converts between strings and state objects.\n *\n * The template requires two functions:\n * - `parse`: A function that takes a string and returns the corresponding state object.\n * - `stringify`: A function that takes a state object and returns a string that represents the given state.\n */\nexport const createStringSchema = <State = never, SerializableState = State>(\n\ttemplate: Omit<StringSchema<State, SerializableState>, \"parseStringify\">,\n): StringSchema<State, SerializableState> => {\n\tconst schema = createSchema<State, string, SerializableState>({\n\t\tserialize: template.stringify,\n\t\tparse: template.parse,\n\t});\n\n\treturn {\n\t\tparse: schema.parse,\n\t\tstringify: schema.serialize,\n\t\tparseStringify: schema.parserialize,\n\t};\n};\n","import { createStringSchema } from \"./string-schema\";\n\n/**\n * Utility schema for parsing and stringifying href strings into structured state objects that include the pathname and URL search parameters.\n *\n * @example\n * ```ts\n * import { createSchema, hrefSchema } from \"@buildinams/core/schema\";\n *\n * const myHrefSchema = createSchema<{\n * page: number,\n * // Used to compute next href - don't use directly. Use `.stringify()` instead\n * _href: string\n * }>({\n * parse: (href) => {\n * const { searchParams } = hrefSchema.parse(href);\n * return {\n * page: parseInt(searchParams.get(\"page\") || \"1\", 10),\n * _href: href,\n * }\n * },\n * serialize: (state) => {\n * const { pathname, searchParams } = hrefSchema.parse(state._href);\n * searchParams.delete(\"page\");\n * if (state.page > 1) {\n * searchParams.set(\"page\", state.page.toString());\n * }\n *\n * // note that `searchParams` are optional for serialization\n * return hrefSchema.stringify({ pathname, searchParams });\n * }\n * });\n * ```\n */\nexport const hrefSchema = createStringSchema<\n\t{\n\t\tpathname: string;\n\t\tsearchParams: URLSearchParams;\n\t},\n\t{\n\t\tpathname: string;\n\t\tsearchParams?: URLSearchParams;\n\t}\n>({\n\tparse: (href) => {\n\t\tconst [pathname = \"\", search = \"\"] = href.split(\"?\");\n\t\treturn {\n\t\t\tpathname,\n\t\t\tsearchParams: new URLSearchParams(search),\n\t\t};\n\t},\n\tstringify: (state) => {\n\t\tif (!state.searchParams) return state.pathname;\n\t\tconst paramsString = state.searchParams.toString();\n\t\treturn state.pathname + (paramsString ? `?${paramsString}` : \"\");\n\t},\n});\n"]}
|
package/dist/navigation/index.js
CHANGED
package/dist/schema/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { S as Schema } from '../schema-
|
|
2
|
-
export { b as SchemaSerialized, a as SchemaState,
|
|
1
|
+
import { S as Schema } from '../schema-CfQvhbuU.js';
|
|
2
|
+
export { c as SchemaSerializableState, b as SchemaSerialized, a as SchemaState, d as createSchema } from '../schema-CfQvhbuU.js';
|
|
3
3
|
|
|
4
|
-
type StringSchema<State = never> = {
|
|
5
|
-
parse: Schema<State, string>["parse"];
|
|
6
|
-
stringify: Schema<State, string>["serialize"];
|
|
7
|
-
parseStringify: Schema<State, string>["parserialize"];
|
|
4
|
+
type StringSchema<State = never, SerializableState = State> = {
|
|
5
|
+
parse: Schema<State, string, SerializableState>["parse"];
|
|
6
|
+
stringify: Schema<State, string, SerializableState>["serialize"];
|
|
7
|
+
parseStringify: Schema<State, string, SerializableState>["parserialize"];
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
10
|
* Helper function to create a `StringSchema` which converts between strings and state objects.
|
|
@@ -13,7 +13,7 @@ type StringSchema<State = never> = {
|
|
|
13
13
|
* - `parse`: A function that takes a string and returns the corresponding state object.
|
|
14
14
|
* - `stringify`: A function that takes a state object and returns a string that represents the given state.
|
|
15
15
|
*/
|
|
16
|
-
declare const createStringSchema: <
|
|
16
|
+
declare const createStringSchema: <State = never, SerializableState = State>(template: Omit<StringSchema<State, SerializableState>, "parseStringify">) => StringSchema<State, SerializableState>;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Utility schema for parsing and stringifying href strings into structured state objects that include the pathname and URL search parameters.
|
|
@@ -30,7 +30,7 @@ declare const createStringSchema: <S = never>(template: Omit<StringSchema<S>, "p
|
|
|
30
30
|
* parse: (href) => {
|
|
31
31
|
* const { searchParams } = hrefSchema.parse(href);
|
|
32
32
|
* return {
|
|
33
|
-
* page: parseInt(searchParams.get("page")
|
|
33
|
+
* page: parseInt(searchParams.get("page") || "1", 10),
|
|
34
34
|
* _href: href,
|
|
35
35
|
* }
|
|
36
36
|
* },
|
|
@@ -40,7 +40,9 @@ declare const createStringSchema: <S = never>(template: Omit<StringSchema<S>, "p
|
|
|
40
40
|
* if (state.page > 1) {
|
|
41
41
|
* searchParams.set("page", state.page.toString());
|
|
42
42
|
* }
|
|
43
|
-
*
|
|
43
|
+
*
|
|
44
|
+
* // note that `searchParams` are optional for serialization
|
|
45
|
+
* return hrefSchema.stringify({ pathname, searchParams });
|
|
44
46
|
* }
|
|
45
47
|
* });
|
|
46
48
|
* ```
|
|
@@ -48,6 +50,9 @@ declare const createStringSchema: <S = never>(template: Omit<StringSchema<S>, "p
|
|
|
48
50
|
declare const hrefSchema: StringSchema<{
|
|
49
51
|
pathname: string;
|
|
50
52
|
searchParams: URLSearchParams;
|
|
53
|
+
}, {
|
|
54
|
+
pathname: string;
|
|
55
|
+
searchParams?: URLSearchParams;
|
|
51
56
|
}>;
|
|
52
57
|
|
|
53
58
|
export { Schema, type StringSchema, createStringSchema, hrefSchema };
|
package/dist/schema/index.js
CHANGED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the schema for parsing and stringifying states to and from primitive values.
|
|
3
|
+
*
|
|
4
|
+
* The `Schema` type is a generic type that takes:
|
|
5
|
+
* - `State`: A state object type.
|
|
6
|
+
* - `Serialized`: The raw value type
|
|
7
|
+
* - `SerializableState?`: (optional) A (partial) state object type that can be used for serialization. Defaults to `State` if not provided.
|
|
8
|
+
*
|
|
9
|
+
* It returns the following methods:
|
|
10
|
+
* - `parse`: A function that takes a raw value and returns the corresponding state object.
|
|
11
|
+
* - `serialize`: A function that takes a (serializable) state object and returns a raw value that represents the given state.
|
|
12
|
+
* - `parserialize`: A function that takes a raw value and a reducer function - it returns a new raw value that represents the reduced state.
|
|
13
|
+
*
|
|
14
|
+
* @see createSchema for a helper function to create a `Schema` from simpler parsing and serializing functions.
|
|
15
|
+
*/
|
|
16
|
+
type Schema<State = any, Serialized = any, SerializableState = State> = {
|
|
17
|
+
/** Parse a raw value into the state shape */
|
|
18
|
+
parse: (value: Serialized) => State;
|
|
19
|
+
/** Serialize a state shape into a raw value */
|
|
20
|
+
serialize: (state: SerializableState) => Serialized;
|
|
21
|
+
/** Compute a new serialized value based on a given serialized value and a reducer of the current state. */
|
|
22
|
+
parserialize: (value: Serialized, next: (state: State) => SerializableState) => Serialized;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* A utility type to extract the state shape from a given `Schema`.
|
|
26
|
+
*/
|
|
27
|
+
type SchemaState<T extends Schema> = T extends Schema<infer U> ? U : never;
|
|
28
|
+
/**
|
|
29
|
+
* A utility type to extract the serialized value type from a given `Schema`.
|
|
30
|
+
*/
|
|
31
|
+
type SchemaSerialized<T extends Schema> = T extends Schema<any, infer U> ? U : never;
|
|
32
|
+
/**
|
|
33
|
+
* A utility type to extract the serializable state shape from a given `Schema`.
|
|
34
|
+
*/
|
|
35
|
+
type SchemaSerializableState<T extends Schema> = T extends Schema<any, any, infer U> ? U : never;
|
|
36
|
+
/**
|
|
37
|
+
* Low-level helper function to create a basic `Schema` that receives the config for converting between serialized values and state objects.
|
|
38
|
+
*
|
|
39
|
+
* The template requires two methods:
|
|
40
|
+
* - `parse`: A method that takes a serialized value and returns the corresponding state object.
|
|
41
|
+
* - `serialize`: A method that takes a state object and returns a serialized value that represents the given state.
|
|
42
|
+
*
|
|
43
|
+
* The `parserialize` method is automatically implemented based on the provided `parse` and `serialize` methods, allowing you to compute a new serialized value based on an existing serialized value and a reducer function for the state.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { createSchema } from "@buildinams/core/schema";
|
|
48
|
+
*
|
|
49
|
+
* const numberSchema = createSchema<
|
|
50
|
+
* number, // State
|
|
51
|
+
* string // Serialized
|
|
52
|
+
* >({
|
|
53
|
+
* parse: (value) => parseInt(value, 10),
|
|
54
|
+
* serialize: (state) => state.toString(),
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* const result = numberSchema.parserialize("42", (state) => state + 1);
|
|
58
|
+
* console.log(result); // "43"
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { createSchema } from "@buildinams/core/schema";
|
|
64
|
+
*
|
|
65
|
+
* const hrefSchema: createSchema<
|
|
66
|
+
* { pathname: string, searchParams: URLSearchParams },
|
|
67
|
+
* string,
|
|
68
|
+
* { pathname: string, searchParams?: URLSearchParams } // SerializableState allows to define different state used for serialization, in this case making searchParams optional to allow for more flexible stringification logic
|
|
69
|
+
* >({
|
|
70
|
+
* parse: (href) => {
|
|
71
|
+
* const [pathname = "", search = ""] = href.split("?");
|
|
72
|
+
* return {
|
|
73
|
+
* pathname,
|
|
74
|
+
* searchParams: new URLSearchParams(search),
|
|
75
|
+
* };
|
|
76
|
+
* },
|
|
77
|
+
* serialize: (serializableState) => {
|
|
78
|
+
* if (!serializableState.searchParams) return serializableState.pathname;
|
|
79
|
+
* const paramsString = serializableState.searchParams.toString();
|
|
80
|
+
* return serializableState.pathname + (paramsString ? `?${paramsString}` : "");
|
|
81
|
+
* },
|
|
82
|
+
* })
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
declare const createSchema: <State = never, Serialized = never, SerializableState = State>(template: Omit<Schema<State, Serialized, SerializableState>, "parserialize">) => Schema<State, Serialized, SerializableState>;
|
|
86
|
+
|
|
87
|
+
export { type Schema as S, type SchemaState as a, type SchemaSerialized as b, type SchemaSerializableState as c, createSchema as d };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buildinams/core",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "Build in Amsterdam <development@buildinamsterdam.com> (https://www.buildinamsterdam.com/)",
|
|
6
6
|
"repository": {
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"README.md"
|
|
60
60
|
],
|
|
61
61
|
"publishConfig": {
|
|
62
|
-
"registry": "https://
|
|
62
|
+
"registry": "https://registry.npmjs.org",
|
|
63
63
|
"access": "public"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/schema/schema.ts","../src/schema/string-schema.ts","../src/schema/href-schema.ts"],"names":[],"mappings":";AA4CO,IAAM,YAAA,GAAe,CAC3B,QAAA,MACmB;AAAA,EACnB,GAAG,QAAA;AAAA,EAEH,YAAA,EAAc,CAAC,KAAA,EAAO,IAAA,KAAS;AAC9B,IAAA,OAAO,SAAS,SAAA,CAAU,IAAA,CAAK,SAAS,KAAA,CAAM,KAAK,CAAC,CAAC,CAAA;AAAA,EACtD;AACD,CAAA;;;ACpCO,IAAM,kBAAA,GAAqB,CACjC,QAAA,KACqB;AACrB,EAAA,MAAM,SAAS,YAAA,CAAwB;AAAA,IACtC,WAAW,QAAA,CAAS,SAAA;AAAA,IACpB,OAAO,QAAA,CAAS;AAAA,GAChB,CAAA;AAED,EAAA,OAAO;AAAA,IACN,OAAO,MAAA,CAAO,KAAA;AAAA,IACd,WAAW,MAAA,CAAO,SAAA;AAAA,IAClB,gBAAgB,MAAA,CAAO;AAAA,GACxB;AACD;;;ACGO,IAAM,aAAa,kBAAA,CAGvB;AAAA,EACF,KAAA,EAAO,CAAC,IAAA,KAAS;AAChB,IAAA,MAAM,CAAC,WAAW,EAAA,EAAI,MAAA,GAAS,EAAE,CAAA,GAAI,IAAA,CAAK,MAAM,GAAG,CAAA;AACnD,IAAA,OAAO;AAAA,MACN,QAAA;AAAA,MACA,YAAA,EAAc,IAAI,eAAA,CAAgB,MAAM;AAAA,KACzC;AAAA,EACD,CAAA;AAAA,EACA,SAAA,EAAW,CAAC,KAAA,KAAU;AACrB,IAAA,MAAM,YAAA,GAAe,KAAA,CAAM,YAAA,CAAa,QAAA,EAAS;AACjD,IAAA,OAAO,KAAA,CAAM,QAAA,IAAY,YAAA,GAAe,CAAA,CAAA,EAAI,YAAY,CAAA,CAAA,GAAK,EAAA,CAAA;AAAA,EAC9D;AACD,CAAC","file":"chunk-IAAQVL6M.js","sourcesContent":["/**\n * Defines the schema for parsing and stringifying states to and from primitive values.\n *\n * The `Schema` type is a generic that takes a state shape and optional raw value type and declares three methods:\n *\n * - `parse`: A function that takes a raw value and returns the corresponding state object.\n * - `stringify`: A function that takes a state object and returns a raw value that represents the given state.\n * - `parseStringify`: A function that takes a raw value and a reducer function - it returns a new raw value that represents the reduced state.\n *\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Schema<State = any, Serialized = any> = {\n\t/** Parse a raw value into the state shape */\n\tparse: (value: Serialized) => State;\n\t/** Serialize a state shape into a raw value */\n\tserialize: (state: State) => Serialized;\n\n\t/** Compute a new serialized value based on a given serialized value and a reducer of the current state. */\n\tparserialize: (\n\t\tvalue: Serialized,\n\t\tnext: (state: State) => State,\n\t) => Serialized;\n};\n\n/**\n * A utility type to extract the state shape from a given `Schema`.\n */\nexport type SchemaState<T extends Schema> =\n\tT extends Schema<infer U> ? U : never;\n\n/**\n * A utility type to extract the serialized value type from a given `Schema`.\n */\nexport type SchemaSerialized<T extends Schema> =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tT extends Schema<any, infer U> ? U : never;\n\n/**\n * Low-level helper function to create a basic `Schema` that receives the config for converting between serialized values and state objects.\n *\n * The template requires two functions:\n * - `parse`: A function that takes a serialized value and returns the corresponding state object.\n * - `serialize`: A function that takes a state object and returns a serialized value that represents the given state.\n */\nexport const createSchema = <S = never, R = never>(\n\ttemplate: Omit<Schema<S, R>, \"parserialize\">,\n): Schema<S, R> => ({\n\t...template,\n\n\tparserialize: (value, next) => {\n\t\treturn template.serialize(next(template.parse(value)));\n\t},\n});\n","import type { Schema } from \"./schema\";\nimport { createSchema } from \"./schema\";\n\nexport type StringSchema<State = never> = {\n\tparse: Schema<State, string>[\"parse\"];\n\tstringify: Schema<State, string>[\"serialize\"];\n\tparseStringify: Schema<State, string>[\"parserialize\"];\n};\n\n/**\n * Helper function to create a `StringSchema` which converts between strings and state objects.\n *\n * The template requires two functions:\n * - `parse`: A function that takes a string and returns the corresponding state object.\n * - `stringify`: A function that takes a state object and returns a string that represents the given state.\n */\nexport const createStringSchema = <S = never>(\n\ttemplate: Omit<StringSchema<S>, \"parseStringify\">,\n): StringSchema<S> => {\n\tconst schema = createSchema<S, string>({\n\t\tserialize: template.stringify,\n\t\tparse: template.parse,\n\t});\n\n\treturn {\n\t\tparse: schema.parse,\n\t\tstringify: schema.serialize,\n\t\tparseStringify: schema.parserialize,\n\t};\n};\n","import { createStringSchema } from \"./string-schema\";\n\n/**\n * Utility schema for parsing and stringifying href strings into structured state objects that include the pathname and URL search parameters.\n *\n * @example\n * ```ts\n * import { createSchema, hrefSchema } from \"@buildinams/core/schema\";\n *\n * const myHrefSchema = createSchema<{\n * page: number,\n * // Used to compute next href - don't use directly. Use `.stringify()` instead\n * _href: string\n * }>({\n * parse: (href) => {\n * const { searchParams } = hrefSchema.parse(href);\n * return {\n * page: parseInt(searchParams.get(\"page\"), 10) || 1,\n * _href: href,\n * }\n * },\n * serialize: (state) => {\n * const { pathname, searchParams } = hrefSchema.parse(state._href);\n * searchParams.delete(\"page\");\n * if (state.page > 1) {\n * searchParams.set(\"page\", state.page.toString());\n * }\n * return hrefSchema.serialize({ pathname, searchParams });\n * }\n * });\n * ```\n */\nexport const hrefSchema = createStringSchema<{\n\tpathname: string;\n\tsearchParams: URLSearchParams;\n}>({\n\tparse: (href) => {\n\t\tconst [pathname = \"\", search = \"\"] = href.split(\"?\");\n\t\treturn {\n\t\t\tpathname,\n\t\t\tsearchParams: new URLSearchParams(search),\n\t\t};\n\t},\n\tstringify: (state) => {\n\t\tconst paramsString = state.searchParams.toString();\n\t\treturn state.pathname + (paramsString ? `?${paramsString}` : \"\");\n\t},\n});\n"]}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Defines the schema for parsing and stringifying states to and from primitive values.
|
|
3
|
-
*
|
|
4
|
-
* The `Schema` type is a generic that takes a state shape and optional raw value type and declares three methods:
|
|
5
|
-
*
|
|
6
|
-
* - `parse`: A function that takes a raw value and returns the corresponding state object.
|
|
7
|
-
* - `stringify`: A function that takes a state object and returns a raw value that represents the given state.
|
|
8
|
-
* - `parseStringify`: A function that takes a raw value and a reducer function - it returns a new raw value that represents the reduced state.
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
type Schema<State = any, Serialized = any> = {
|
|
12
|
-
/** Parse a raw value into the state shape */
|
|
13
|
-
parse: (value: Serialized) => State;
|
|
14
|
-
/** Serialize a state shape into a raw value */
|
|
15
|
-
serialize: (state: State) => Serialized;
|
|
16
|
-
/** Compute a new serialized value based on a given serialized value and a reducer of the current state. */
|
|
17
|
-
parserialize: (value: Serialized, next: (state: State) => State) => Serialized;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* A utility type to extract the state shape from a given `Schema`.
|
|
21
|
-
*/
|
|
22
|
-
type SchemaState<T extends Schema> = T extends Schema<infer U> ? U : never;
|
|
23
|
-
/**
|
|
24
|
-
* A utility type to extract the serialized value type from a given `Schema`.
|
|
25
|
-
*/
|
|
26
|
-
type SchemaSerialized<T extends Schema> = T extends Schema<any, infer U> ? U : never;
|
|
27
|
-
/**
|
|
28
|
-
* Low-level helper function to create a basic `Schema` that receives the config for converting between serialized values and state objects.
|
|
29
|
-
*
|
|
30
|
-
* The template requires two functions:
|
|
31
|
-
* - `parse`: A function that takes a serialized value and returns the corresponding state object.
|
|
32
|
-
* - `serialize`: A function that takes a state object and returns a serialized value that represents the given state.
|
|
33
|
-
*/
|
|
34
|
-
declare const createSchema: <S = never, R = never>(template: Omit<Schema<S, R>, "parserialize">) => Schema<S, R>;
|
|
35
|
-
|
|
36
|
-
export { type Schema as S, type SchemaState as a, type SchemaSerialized as b, createSchema as c };
|