@haverstack/wire-types 0.5.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/LICENSE +7 -0
- package/README.md +50 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
|
2
|
+
|
|
3
|
+
To the extent possible under law, the Haverstack contributors have waived all
|
|
4
|
+
copyright and related or neighboring rights to Haverstack. This work is
|
|
5
|
+
published from the United States.
|
|
6
|
+
|
|
7
|
+
https://creativecommons.org/publicdomain/zero/1.0/
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# @haverstack/wire-types
|
|
2
|
+
|
|
3
|
+
HTTP wire types and serialization utilities for the Haverstack API.
|
|
4
|
+
|
|
5
|
+
This package defines the JSON-safe representations of Haverstack's core domain types — `Date` fields become ISO strings — and provides functions to serialize domain objects into that format and parse dates back out. It is the shared contract between `haverstack/server` (which serializes responses) and `@haverstack/adapter-api` (which parses them).
|
|
6
|
+
|
|
7
|
+
> **Status:** Early development. APIs are unstable.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @haverstack/wire-types
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Wire types
|
|
16
|
+
|
|
17
|
+
Each wire type mirrors a domain type from `@haverstack/core` with `Date` fields replaced by ISO 8601 strings.
|
|
18
|
+
|
|
19
|
+
| Wire type | Domain type |
|
|
20
|
+
|-----------|-------------|
|
|
21
|
+
| `WireRecord` | `StackRecord` |
|
|
22
|
+
| `WireType` | `StackType` |
|
|
23
|
+
| `WireVersion` | `RecordVersion` |
|
|
24
|
+
|
|
25
|
+
## Serialization
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import {
|
|
29
|
+
serializeRecord,
|
|
30
|
+
serializeType,
|
|
31
|
+
serializeVersion,
|
|
32
|
+
parseDate,
|
|
33
|
+
} from '@haverstack/wire-types';
|
|
34
|
+
|
|
35
|
+
// Server side — convert domain objects before sending as JSON
|
|
36
|
+
const body = JSON.stringify(serializeRecord(record));
|
|
37
|
+
|
|
38
|
+
// Client side — convert ISO strings back to Dates after parsing JSON
|
|
39
|
+
const createdAt = parseDate(raw.createdAt); // Date | undefined
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`parseDate` accepts `unknown` and returns `undefined` for missing or unparseable values, making it safe to use directly on unvalidated response fields.
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
[CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/) — public domain.
|
|
47
|
+
|
|
48
|
+
## Monorepo
|
|
49
|
+
|
|
50
|
+
Part of [haverstack/core](https://github.com/haverstack/core).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { StackRecord, StackType, RecordVersion, Association, Permission } from '@haverstack/core';
|
|
2
|
+
export type WireRecord = {
|
|
3
|
+
id: string;
|
|
4
|
+
typeId: string;
|
|
5
|
+
createdAt: string;
|
|
6
|
+
updatedAt: string;
|
|
7
|
+
content: Record<string, unknown>;
|
|
8
|
+
version: number;
|
|
9
|
+
parentId?: string;
|
|
10
|
+
entityId?: string;
|
|
11
|
+
appId?: string;
|
|
12
|
+
deletedAt?: string;
|
|
13
|
+
permissions?: Permission[];
|
|
14
|
+
associations?: Association[];
|
|
15
|
+
};
|
|
16
|
+
export type WireType = {
|
|
17
|
+
id: string;
|
|
18
|
+
baseId: string;
|
|
19
|
+
version: number;
|
|
20
|
+
name: string;
|
|
21
|
+
schema: Record<string, unknown>;
|
|
22
|
+
schemaHash: string;
|
|
23
|
+
migratesFrom?: string;
|
|
24
|
+
createdAt: string;
|
|
25
|
+
};
|
|
26
|
+
export type WireVersion = {
|
|
27
|
+
version: number;
|
|
28
|
+
content: Record<string, unknown>;
|
|
29
|
+
updatedAt: string;
|
|
30
|
+
entityId?: string;
|
|
31
|
+
};
|
|
32
|
+
export declare function serializeRecord(r: StackRecord): WireRecord;
|
|
33
|
+
export declare function serializeType(t: StackType): WireType;
|
|
34
|
+
export declare function serializeVersion(v: RecordVersion): WireVersion;
|
|
35
|
+
/** Parse an ISO date string from a wire body, returns undefined if absent or invalid. */
|
|
36
|
+
export declare function parseDate(val: unknown): Date | undefined;
|
|
37
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,SAAS,EACT,aAAa,EACb,WAAW,EACX,UAAU,EACX,MAAM,kBAAkB,CAAC;AAE1B,MAAM,MAAM,UAAU,GAAG;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;IAC3B,YAAY,CAAC,EAAE,WAAW,EAAE,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,wBAAgB,eAAe,CAAC,CAAC,EAAE,WAAW,GAAG,UAAU,CAgB1D;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,SAAS,GAAG,QAAQ,CAYpD;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,aAAa,GAAG,WAAW,CAQ9D;AAED,yFAAyF;AACzF,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAIxD"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export function serializeRecord(r) {
|
|
2
|
+
const w = {
|
|
3
|
+
id: r.id,
|
|
4
|
+
typeId: r.typeId,
|
|
5
|
+
createdAt: r.createdAt.toISOString(),
|
|
6
|
+
updatedAt: r.updatedAt.toISOString(),
|
|
7
|
+
content: r.content,
|
|
8
|
+
version: r.version,
|
|
9
|
+
};
|
|
10
|
+
if (r.parentId !== undefined)
|
|
11
|
+
w.parentId = r.parentId;
|
|
12
|
+
if (r.entityId !== undefined)
|
|
13
|
+
w.entityId = r.entityId;
|
|
14
|
+
if (r.appId !== undefined)
|
|
15
|
+
w.appId = r.appId;
|
|
16
|
+
if (r.deletedAt !== undefined)
|
|
17
|
+
w.deletedAt = r.deletedAt.toISOString();
|
|
18
|
+
if (r.permissions !== undefined)
|
|
19
|
+
w.permissions = r.permissions;
|
|
20
|
+
if (r.associations !== undefined)
|
|
21
|
+
w.associations = r.associations;
|
|
22
|
+
return w;
|
|
23
|
+
}
|
|
24
|
+
export function serializeType(t) {
|
|
25
|
+
const w = {
|
|
26
|
+
id: t.id,
|
|
27
|
+
baseId: t.baseId,
|
|
28
|
+
version: t.version,
|
|
29
|
+
name: t.name,
|
|
30
|
+
schema: t.schema,
|
|
31
|
+
schemaHash: t.schemaHash,
|
|
32
|
+
createdAt: t.createdAt.toISOString(),
|
|
33
|
+
};
|
|
34
|
+
if (t.migratesFrom !== undefined)
|
|
35
|
+
w.migratesFrom = t.migratesFrom;
|
|
36
|
+
return w;
|
|
37
|
+
}
|
|
38
|
+
export function serializeVersion(v) {
|
|
39
|
+
const w = {
|
|
40
|
+
version: v.version,
|
|
41
|
+
content: v.content,
|
|
42
|
+
updatedAt: v.updatedAt.toISOString(),
|
|
43
|
+
};
|
|
44
|
+
if (v.entityId !== undefined)
|
|
45
|
+
w.entityId = v.entityId;
|
|
46
|
+
return w;
|
|
47
|
+
}
|
|
48
|
+
/** Parse an ISO date string from a wire body, returns undefined if absent or invalid. */
|
|
49
|
+
export function parseDate(val) {
|
|
50
|
+
if (typeof val !== 'string')
|
|
51
|
+
return undefined;
|
|
52
|
+
const d = new Date(val);
|
|
53
|
+
return isNaN(d.getTime()) ? undefined : d;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAyCA,MAAM,UAAU,eAAe,CAAC,CAAc;IAC5C,MAAM,CAAC,GAAe;QACpB,EAAE,EAAE,CAAC,CAAC,EAAE;QACR,MAAM,EAAE,CAAC,CAAC,MAAM;QAChB,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,WAAW,EAAE;QACpC,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,WAAW,EAAE;QACpC,OAAO,EAAE,CAAC,CAAC,OAAO;QAClB,OAAO,EAAE,CAAC,CAAC,OAAO;KACnB,CAAC;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AAC,CAAC,OAAO;QAClB,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,WAAW,EAAE;KACrC,CAAC;IACF,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS;QAAE,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC;IACtD,OAAO,CAAC,CAAC;AACX,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,SAAS,CAAC,GAAY;IACpC,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAC9C,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACxB,OAAO,KAAK,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;AAC5C,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@haverstack/wire-types",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "HTTP wire types and serialization for Haverstack",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@haverstack/core": "0.5.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^22.0.0",
|
|
22
|
+
"typescript": "^5.5.0"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc -p tsconfig.build.json",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"lint": "eslint src"
|
|
28
|
+
}
|
|
29
|
+
}
|