@buildinams/core 0.1.0 → 0.3.0
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 +39 -0
- package/dist/chunk-IAAQVL6M.js.map +1 -0
- package/dist/navigation/index.d.ts +71 -0
- package/dist/navigation/index.js +22 -0
- package/dist/navigation/index.js.map +1 -0
- package/dist/schema/index.d.ts +53 -0
- package/dist/schema/index.js +3 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema-iy92gbTC.d.ts +36 -0
- package/package.json +15 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/schema/schema.ts
|
|
2
|
+
var createSchema = (template) => ({
|
|
3
|
+
...template,
|
|
4
|
+
parserialize: (value, next) => {
|
|
5
|
+
return template.serialize(next(template.parse(value)));
|
|
6
|
+
}
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/schema/string-schema.ts
|
|
10
|
+
var createStringSchema = (template) => {
|
|
11
|
+
const schema = createSchema({
|
|
12
|
+
serialize: template.stringify,
|
|
13
|
+
parse: template.parse
|
|
14
|
+
});
|
|
15
|
+
return {
|
|
16
|
+
parse: schema.parse,
|
|
17
|
+
stringify: schema.serialize,
|
|
18
|
+
parseStringify: schema.parserialize
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/schema/href-schema.ts
|
|
23
|
+
var hrefSchema = createStringSchema({
|
|
24
|
+
parse: (href) => {
|
|
25
|
+
const [pathname = "", search = ""] = href.split("?");
|
|
26
|
+
return {
|
|
27
|
+
pathname,
|
|
28
|
+
searchParams: new URLSearchParams(search)
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
stringify: (state) => {
|
|
32
|
+
const paramsString = state.searchParams.toString();
|
|
33
|
+
return state.pathname + (paramsString ? `?${paramsString}` : "");
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export { createSchema, createStringSchema, hrefSchema };
|
|
38
|
+
//# sourceMappingURL=chunk-IAAQVL6M.js.map
|
|
39
|
+
//# sourceMappingURL=chunk-IAAQVL6M.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":";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"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { S as Schema } from '../schema-iy92gbTC.js';
|
|
2
|
+
|
|
3
|
+
/** @deprecated use `Schema` directly */
|
|
4
|
+
type HrefSchema<State = never> = {
|
|
5
|
+
/** Parse a href string into the state shape */
|
|
6
|
+
parse: Schema<State & {
|
|
7
|
+
href: string;
|
|
8
|
+
}, string>["parse"];
|
|
9
|
+
/** Stringify a state shape into a href string */
|
|
10
|
+
stringify: Schema<State & {
|
|
11
|
+
href: string;
|
|
12
|
+
}, string>["serialize"];
|
|
13
|
+
/** Compute a new href string based on a given href string and a reducer of the current state. */
|
|
14
|
+
parseStringify: Schema<State & {
|
|
15
|
+
href: string;
|
|
16
|
+
}, string>["parserialize"];
|
|
17
|
+
};
|
|
18
|
+
/** @deprecated use `SchemaState` directly */
|
|
19
|
+
type HrefSchemaState<T extends HrefSchema> = T extends HrefSchema<infer U> ? U & {
|
|
20
|
+
href: string;
|
|
21
|
+
} : never;
|
|
22
|
+
/**
|
|
23
|
+
* Helper function to create a `HrefSchema` from a simpler template that only requires the logic for converting between URLs and state objects. The `createSchema` function takes care of implementing the `parse`, `stringify`, and `parseStringify` methods based on the provided template functions.
|
|
24
|
+
*
|
|
25
|
+
* The template requires two functions:
|
|
26
|
+
* - `urlToState`: A function that takes a URL object and returns the corresponding state object.
|
|
27
|
+
* - `stateToUrl`: A function that takes a state object and the current URL, and returns a new URL object that represents the given state.
|
|
28
|
+
*
|
|
29
|
+
* This allows you to focus on the logic for how your application's state maps to the URL structure without having to manually handle the parsing and stringifying of hrefs.
|
|
30
|
+
*
|
|
31
|
+
* @deprecated The `createHrefSchema` function is deprecated in favor of using the more flexible `createStringSchema` function directly with custom parsing and stringifying logic. The `createStringSchema` function can be used to achieve the same functionality without being tied specifically to hrefs, allowing for greater reuse and adaptability in different contexts.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { createHrefSchema } from "@buildinams/core/schema";
|
|
36
|
+
*
|
|
37
|
+
* const myHrefSchema = createHrefSchema<{
|
|
38
|
+
* page: number,
|
|
39
|
+
* }>({
|
|
40
|
+
* urlToState: (url) => {
|
|
41
|
+
* return {
|
|
42
|
+
* page: parseInt(url.searchParams.get("page") || "1", 10),
|
|
43
|
+
* }
|
|
44
|
+
* },
|
|
45
|
+
* stateToUrl: (state, url) => {
|
|
46
|
+
* url.searchParams.delete("page");
|
|
47
|
+
* if (state.page > 1) {
|
|
48
|
+
* url.searchParams.set("page", state.page.toString());
|
|
49
|
+
* }
|
|
50
|
+
* return url;
|
|
51
|
+
* }
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* // Using the schema to parse a href string into state
|
|
55
|
+
* const state = myHrefSchema.parse("/products?page=2");
|
|
56
|
+
* console.log(state); // { page: 2, href: "/products?page=2" }
|
|
57
|
+
*
|
|
58
|
+
* // Using the schema to stringify a state object into a href string
|
|
59
|
+
* const href = myHrefSchema.stringify({ page: 3, href: "/products?page=2" });
|
|
60
|
+
* console.log(href); // "/products?page=3"
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare const createHrefSchema: <T>(template: HrefSchemaTemplate<T>) => HrefSchema<T>;
|
|
64
|
+
type HrefSchemaTemplate<State = never> = {
|
|
65
|
+
/** Parse a href string into the state shape */
|
|
66
|
+
urlToState: (url: URL) => State;
|
|
67
|
+
/** Stringify a state shape and current href into a new href string */
|
|
68
|
+
stateToUrl: (state: State, url: URL) => URL;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export { type HrefSchema, type HrefSchemaState, createHrefSchema };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createStringSchema } from '../chunk-IAAQVL6M.js';
|
|
2
|
+
|
|
3
|
+
// src/navigation/href-schema.ts
|
|
4
|
+
var hrefToUrl = (href) => new URL(href, "http://localhost");
|
|
5
|
+
var createHrefSchema = (template) => createStringSchema({
|
|
6
|
+
stringify: (state) => {
|
|
7
|
+
const current = hrefToUrl(state.href);
|
|
8
|
+
const next = template.stateToUrl(state, current);
|
|
9
|
+
return next.pathname + next.search;
|
|
10
|
+
},
|
|
11
|
+
parse: (href) => {
|
|
12
|
+
const current = hrefToUrl(href);
|
|
13
|
+
return {
|
|
14
|
+
...template.urlToState(current),
|
|
15
|
+
href: current.pathname + current.search
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export { createHrefSchema };
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/navigation/href-schema.ts"],"names":[],"mappings":";;;AAMA,IAAM,YAAY,CAAC,IAAA,KAAiB,IAAI,GAAA,CAAI,MAAM,kBAAkB,CAAA;AAyD7D,IAAM,gBAAA,GAAmB,CAC/B,QAAA,KAEA,kBAAA,CAAyC;AAAA,EACxC,SAAA,EAAW,CAAC,KAAA,KAAU;AACrB,IAAA,MAAM,OAAA,GAAU,SAAA,CAAU,KAAA,CAAM,IAAI,CAAA;AACpC,IAAA,MAAM,IAAA,GAAO,QAAA,CAAS,UAAA,CAAW,KAAA,EAAO,OAAO,CAAA;AAC/C,IAAA,OAAO,IAAA,CAAK,WAAW,IAAA,CAAK,MAAA;AAAA,EAC7B,CAAA;AAAA,EAEA,KAAA,EAAO,CAAC,IAAA,KAAS;AAChB,IAAA,MAAM,OAAA,GAAU,UAAU,IAAI,CAAA;AAC9B,IAAA,OAAO;AAAA,MACN,GAAG,QAAA,CAAS,UAAA,CAAW,OAAO,CAAA;AAAA,MAC9B,IAAA,EAAM,OAAA,CAAQ,QAAA,GAAW,OAAA,CAAQ;AAAA,KAClC;AAAA,EACD;AACD,CAAC","file":"index.js","sourcesContent":["// This is fine as we only use the `pathname` an `search` properties from the URL object,\n\nimport type { Schema } from \"../schema\";\nimport { createStringSchema } from \"../schema\";\n\n// which do not contain the \"https://localhost\" origin.\nconst hrefToUrl = (href: string) => new URL(href, \"http://localhost\");\n\n/** @deprecated use `Schema` directly */\nexport type HrefSchema<State = never> = {\n\t/** Parse a href string into the state shape */\n\tparse: Schema<State & { href: string }, string>[\"parse\"];\n\t/** Stringify a state shape into a href string */\n\tstringify: Schema<State & { href: string }, string>[\"serialize\"];\n\t/** Compute a new href string based on a given href string and a reducer of the current state. */\n\tparseStringify: Schema<State & { href: string }, string>[\"parserialize\"];\n};\n\n/** @deprecated use `SchemaState` directly */\nexport type HrefSchemaState<T extends HrefSchema> =\n\tT extends HrefSchema<infer U> ? U & { href: string } : never;\n\n/**\n * Helper function to create a `HrefSchema` from a simpler template that only requires the logic for converting between URLs and state objects. The `createSchema` function takes care of implementing the `parse`, `stringify`, and `parseStringify` methods based on the provided template functions.\n *\n * The template requires two functions:\n * - `urlToState`: A function that takes a URL object and returns the corresponding state object.\n * - `stateToUrl`: A function that takes a state object and the current URL, and returns a new URL object that represents the given state.\n *\n * This allows you to focus on the logic for how your application's state maps to the URL structure without having to manually handle the parsing and stringifying of hrefs.\n *\n * @deprecated The `createHrefSchema` function is deprecated in favor of using the more flexible `createStringSchema` function directly with custom parsing and stringifying logic. The `createStringSchema` function can be used to achieve the same functionality without being tied specifically to hrefs, allowing for greater reuse and adaptability in different contexts.\n *\n * @example\n * ```ts\n * import { createHrefSchema } from \"@buildinams/core/schema\";\n *\n * const myHrefSchema = createHrefSchema<{\n * page: number,\n * }>({\n * urlToState: (url) => {\n * return {\n * page: parseInt(url.searchParams.get(\"page\") || \"1\", 10),\n * }\n * },\n * stateToUrl: (state, url) => {\n * url.searchParams.delete(\"page\");\n * if (state.page > 1) {\n * url.searchParams.set(\"page\", state.page.toString());\n * }\n * return url;\n * }\n * });\n *\n * // Using the schema to parse a href string into state\n * const state = myHrefSchema.parse(\"/products?page=2\");\n * console.log(state); // { page: 2, href: \"/products?page=2\" }\n *\n * // Using the schema to stringify a state object into a href string\n * const href = myHrefSchema.stringify({ page: 3, href: \"/products?page=2\" });\n * console.log(href); // \"/products?page=3\"\n * ```\n */\nexport const createHrefSchema = <T>(\n\ttemplate: HrefSchemaTemplate<T>,\n): HrefSchema<T> =>\n\tcreateStringSchema<T & { href: string }>({\n\t\tstringify: (state) => {\n\t\t\tconst current = hrefToUrl(state.href);\n\t\t\tconst next = template.stateToUrl(state, current);\n\t\t\treturn next.pathname + next.search;\n\t\t},\n\n\t\tparse: (href) => {\n\t\t\tconst current = hrefToUrl(href);\n\t\t\treturn {\n\t\t\t\t...template.urlToState(current),\n\t\t\t\thref: current.pathname + current.search,\n\t\t\t};\n\t\t},\n\t});\n\ntype HrefSchemaTemplate<State = never> = {\n\t/** Parse a href string into the state shape */\n\turlToState: (url: URL) => State;\n\t/** Stringify a state shape and current href into a new href string */\n\tstateToUrl: (state: State, url: URL) => URL;\n};\n"]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { S as Schema } from '../schema-iy92gbTC.js';
|
|
2
|
+
export { b as SchemaSerialized, a as SchemaState, c as createSchema } from '../schema-iy92gbTC.js';
|
|
3
|
+
|
|
4
|
+
type StringSchema<State = never> = {
|
|
5
|
+
parse: Schema<State, string>["parse"];
|
|
6
|
+
stringify: Schema<State, string>["serialize"];
|
|
7
|
+
parseStringify: Schema<State, string>["parserialize"];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Helper function to create a `StringSchema` which converts between strings and state objects.
|
|
11
|
+
*
|
|
12
|
+
* The template requires two functions:
|
|
13
|
+
* - `parse`: A function that takes a string and returns the corresponding state object.
|
|
14
|
+
* - `stringify`: A function that takes a state object and returns a string that represents the given state.
|
|
15
|
+
*/
|
|
16
|
+
declare const createStringSchema: <S = never>(template: Omit<StringSchema<S>, "parseStringify">) => StringSchema<S>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Utility schema for parsing and stringifying href strings into structured state objects that include the pathname and URL search parameters.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { createSchema, hrefSchema } from "@buildinams/core/schema";
|
|
24
|
+
*
|
|
25
|
+
* const myHrefSchema = createSchema<{
|
|
26
|
+
* page: number,
|
|
27
|
+
* // Used to compute next href - don't use directly. Use `.stringify()` instead
|
|
28
|
+
* _href: string
|
|
29
|
+
* }>({
|
|
30
|
+
* parse: (href) => {
|
|
31
|
+
* const { searchParams } = hrefSchema.parse(href);
|
|
32
|
+
* return {
|
|
33
|
+
* page: parseInt(searchParams.get("page"), 10) || 1,
|
|
34
|
+
* _href: href,
|
|
35
|
+
* }
|
|
36
|
+
* },
|
|
37
|
+
* serialize: (state) => {
|
|
38
|
+
* const { pathname, searchParams } = hrefSchema.parse(state._href);
|
|
39
|
+
* searchParams.delete("page");
|
|
40
|
+
* if (state.page > 1) {
|
|
41
|
+
* searchParams.set("page", state.page.toString());
|
|
42
|
+
* }
|
|
43
|
+
* return hrefSchema.serialize({ pathname, searchParams });
|
|
44
|
+
* }
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare const hrefSchema: StringSchema<{
|
|
49
|
+
pathname: string;
|
|
50
|
+
searchParams: URLSearchParams;
|
|
51
|
+
}>;
|
|
52
|
+
|
|
53
|
+
export { Schema, type StringSchema, createStringSchema, hrefSchema };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,36 @@
|
|
|
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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buildinams/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "Build in Amsterdam <development@buildinamsterdam.com> (https://www.buildinamsterdam.com/)",
|
|
6
6
|
"repository": {
|
|
@@ -25,6 +25,14 @@
|
|
|
25
25
|
"types": "./dist/index.d.ts",
|
|
26
26
|
"default": "./dist/index.js"
|
|
27
27
|
},
|
|
28
|
+
"./navigation": {
|
|
29
|
+
"types": "./dist/navigation/index.d.ts",
|
|
30
|
+
"default": "./dist/navigation/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./schema": {
|
|
33
|
+
"types": "./dist/schema/index.d.ts",
|
|
34
|
+
"default": "./dist/schema/index.js"
|
|
35
|
+
},
|
|
28
36
|
"./store": {
|
|
29
37
|
"types": "./dist/store/index.d.ts",
|
|
30
38
|
"default": "./dist/store/index.js"
|
|
@@ -35,6 +43,12 @@
|
|
|
35
43
|
"*": [
|
|
36
44
|
"./dist/index.d.ts"
|
|
37
45
|
],
|
|
46
|
+
"./navigation": [
|
|
47
|
+
"./dist/navigation/index.d.ts"
|
|
48
|
+
],
|
|
49
|
+
"./schema": [
|
|
50
|
+
"./dist/schema/index.d.ts"
|
|
51
|
+
],
|
|
38
52
|
"./store": [
|
|
39
53
|
"./dist/store/index.d.ts"
|
|
40
54
|
]
|