@cluesurf/kink 1.0.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.
@@ -0,0 +1,50 @@
1
+ import { CustomError } from 'ts-custom-error';
2
+ export type BaseHook<T extends any = any> = (take?: T) => KinkMeshBase & Link;
3
+ export type FillHook<T extends any = any, U extends any = any> = (take?: T, load?: U) => Link;
4
+ export type KinkMesh = {
5
+ code: string;
6
+ form: string;
7
+ host: string;
8
+ link?: Link;
9
+ note: string;
10
+ siteCode?: number;
11
+ take?: Link;
12
+ time: string;
13
+ };
14
+ export type KinkMeshBase = {
15
+ code: number;
16
+ note: string;
17
+ };
18
+ export type Link = Record<string, unknown>;
19
+ export type LoadHook<T extends any = any> = (take?: T) => Link;
20
+ export default class Kink extends CustomError {
21
+ form: string;
22
+ host: string;
23
+ code: string;
24
+ note: string;
25
+ link: Link;
26
+ siteCode?: number;
27
+ take?: Link;
28
+ time: string;
29
+ static base: (host: string, form: string, hook: BaseHook) => typeof Kink;
30
+ static code: (host: string, hook: (code: number) => string) => typeof Kink;
31
+ static load: (host: string, form: string, hook: LoadHook) => typeof Kink;
32
+ static fill: (host: string, form: string, hook: FillHook) => typeof Kink;
33
+ static time: (hook: TimeHook) => typeof Kink;
34
+ static makeTime: (time: number) => string;
35
+ static make: (host: string, form: string, take?: any) => Kink;
36
+ static saveLoad: (kink: Kink, take?: any) => void;
37
+ static saveFill: (kink: Kink) => void;
38
+ static makeCode: (host: string, codeLink: number) => string;
39
+ static makeBase: (kink: Error, { siteCode, list, }?: {
40
+ list?: boolean | undefined;
41
+ siteCode?: number | undefined;
42
+ }) => Kink;
43
+ constructor({ siteCode, host, note, form, take, link, code, time, }: KinkMesh);
44
+ toJSON(): KinkMesh;
45
+ }
46
+ export type TimeHook = (time: number) => string;
47
+ export declare class KinkList extends Kink {
48
+ list: Array<Kink>;
49
+ constructor(list: Array<Kink>);
50
+ }
package/host/index.js ADDED
@@ -0,0 +1,147 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { CustomError } from 'ts-custom-error';
3
+ const base = {};
4
+ const load = {};
5
+ const fill = {};
6
+ const code = {};
7
+ let timeHook = (time) => String(time);
8
+ class Kink extends CustomError {
9
+ form;
10
+ host;
11
+ code;
12
+ note;
13
+ link;
14
+ siteCode;
15
+ take;
16
+ time;
17
+ static base = (host, form, hook) => {
18
+ base[`${host}:${form}`] = hook;
19
+ return Kink;
20
+ };
21
+ static code = (host, hook) => {
22
+ code[host] = hook;
23
+ return Kink;
24
+ };
25
+ static load = (host, form, hook) => {
26
+ load[`${host}:${form}`] = hook;
27
+ return Kink;
28
+ };
29
+ static fill = (host, form, hook) => {
30
+ fill[`${host}:${form}`] = hook;
31
+ return Kink;
32
+ };
33
+ static time = (hook) => {
34
+ timeHook = hook;
35
+ return Kink;
36
+ };
37
+ static makeTime = (time) => {
38
+ return timeHook(time);
39
+ };
40
+ static make = (host, form, take) => {
41
+ const time = Kink.makeTime(Date.now());
42
+ const hook = base[`${host}:${form}`];
43
+ if (!hook) {
44
+ throw new Error(`Missing ${host}:${form} in Kink.base`);
45
+ }
46
+ const hookLink = hook(take);
47
+ const kink = new Kink({
48
+ ...hookLink,
49
+ code: Kink.makeCode(host, hookLink.code),
50
+ form,
51
+ host,
52
+ take: take,
53
+ time,
54
+ });
55
+ Kink.saveLoad(kink, take);
56
+ return kink;
57
+ };
58
+ static saveLoad = (kink, take) => {
59
+ const hook = load[`${kink.host}:${kink.form}`];
60
+ if (!hook) {
61
+ // throw new Error(`Missing ${kink.host}:${kink.form} in Kink.load`)
62
+ return;
63
+ }
64
+ kink.link = hook(take);
65
+ };
66
+ static saveFill = (kink) => {
67
+ const hook = fill[`${kink.host}:${kink.form}`];
68
+ if (!hook) {
69
+ // throw new Error(`Missing ${kink.host}:${kink.form} in Kink.fill`)
70
+ return;
71
+ }
72
+ kink.link = hook(kink.take, kink.link);
73
+ };
74
+ static makeCode = (host, codeLink) => {
75
+ const hook = code[host];
76
+ if (!hook) {
77
+ return codeLink.toString();
78
+ }
79
+ return hook(codeLink);
80
+ };
81
+ static makeBase = (kink, { siteCode, list = false, } = {}) => {
82
+ const time = Kink.makeTime(Date.now());
83
+ return new Kink({
84
+ code: 'code' in kink
85
+ ? typeof kink.code === 'string'
86
+ ? kink.code
87
+ : typeof kink.code === 'number'
88
+ ? Kink.makeCode('system', kink.code)
89
+ : '0000'
90
+ : '0000',
91
+ form: 'system_error',
92
+ host: 'system',
93
+ // list: stack ? kink.stack?.split('\n') ?? [] : [],
94
+ note: kink.message,
95
+ siteCode,
96
+ time,
97
+ });
98
+ };
99
+ constructor({ siteCode, host, note, form, take, link = {}, code, time, }) {
100
+ super(note);
101
+ Object.defineProperty(this, 'name', {
102
+ enumerable: false,
103
+ value: '',
104
+ writable: true,
105
+ });
106
+ Object.defineProperty(this, 'take', {
107
+ enumerable: false,
108
+ value: take,
109
+ writable: true,
110
+ });
111
+ this.time = time;
112
+ this.host = host;
113
+ this.form = form;
114
+ this.code = code;
115
+ this.note = note;
116
+ this.link = link;
117
+ this.take = take;
118
+ this.siteCode = siteCode;
119
+ }
120
+ toJSON() {
121
+ return {
122
+ code: this.code,
123
+ form: this.form,
124
+ host: this.host,
125
+ link: this.link,
126
+ note: this.note,
127
+ time: this.time,
128
+ };
129
+ }
130
+ }
131
+ export default Kink;
132
+ // eslint-disable-next-line sort-exports/sort-exports
133
+ export class KinkList extends Kink {
134
+ list;
135
+ constructor(list) {
136
+ const time = Kink.makeTime(Date.now());
137
+ super({
138
+ code: Kink.makeCode('@termsurf/kink', 0),
139
+ form: 'list',
140
+ host: '@termsurf/kink',
141
+ note: 'A set of errors occurred.',
142
+ time,
143
+ });
144
+ this.list = list;
145
+ }
146
+ }
147
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,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,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;QACtC,KAAK,CAAC;YACJ,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,gBAAgB,EAAE,CAAC,CAAC;YACxC,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,2BAA2B;YACjC,IAAI;SACL,CAAC,CAAA;QACF,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@cluesurf/kink",
3
+ "type": "module",
4
+ "version": "1.0.0",
5
+ "main": "./host/index.js",
6
+ "license": "MIT",
7
+ "files": [
8
+ "./host/index.js",
9
+ "./host/index.js.map",
10
+ "./host/index.d.ts"
11
+ ],
12
+ "scripts": {
13
+ "make": "tsc && tsc-alias",
14
+ "scan": "concurrently --kill-others \"tsc -w\" \"tsc-alias -w\"",
15
+ "lint": "eslint --ext .ts ./make",
16
+ "lint:fix": "npm run lint -- --fix",
17
+ "test": "tsx test.ts",
18
+ "host": "pnpm make && npm publish --access=public"
19
+ },
20
+ "devDependencies": {
21
+ "@trivago/prettier-plugin-sort-imports": "^4.0.0",
22
+ "@types/lodash": "^4.14.194",
23
+ "@types/node": "^18.11.17",
24
+ "@types/prettier": "^2.7.2",
25
+ "@typescript-eslint/eslint-plugin": "^5.46.1",
26
+ "@typescript-eslint/parser": "^5.46.1",
27
+ "concurrently": "^7.6.0",
28
+ "eslint": "^8.31.0",
29
+ "eslint-config-prettier": "^8.5.0",
30
+ "eslint-config-standard-with-typescript": "^24.0.0",
31
+ "eslint-import-resolver-typescript": "^3.5.2",
32
+ "eslint-plugin-import": "^2.26.0",
33
+ "eslint-plugin-n": "^15.6.0",
34
+ "eslint-plugin-prettier": "^4.2.1",
35
+ "eslint-plugin-promise": "^6.1.1",
36
+ "eslint-plugin-simple-import-sort": "^8.0.0",
37
+ "eslint-plugin-sort-exports": "^0.8.0",
38
+ "eslint-plugin-sort-keys": "^2.3.5",
39
+ "eslint-plugin-typescript-sort-keys": "^2.1.0",
40
+ "prettier": "^2.8.1",
41
+ "ts-node": "^10.9.1",
42
+ "tsc-alias": "^1.8.2",
43
+ "tsx": "^4.11.0",
44
+ "typescript": "^5.0.4"
45
+ },
46
+ "dependencies": {
47
+ "ts-custom-error": "^3.3.1"
48
+ }
49
+ }
package/readme.md ADDED
@@ -0,0 +1,72 @@
1
+ <br/>
2
+ <br/>
3
+ <br/>
4
+ <br/>
5
+ <br/>
6
+ <br/>
7
+ <br/>
8
+
9
+ <h3 align='center'>@cluesurf/kink</h3>
10
+ <p align='center'>
11
+ Standard Error Creation in TypeScript
12
+ </p>
13
+
14
+ <br/>
15
+ <br/>
16
+ <br/>
17
+
18
+ ## Installation
19
+
20
+ ```
21
+ pnpm add @cluesurf/kink
22
+ yarn add @cluesurf/kink
23
+ npm i @cluesurf/kink
24
+ ```
25
+
26
+ ## Example
27
+
28
+ ```ts
29
+ import Kink from '@cluesurf/kink'
30
+
31
+ const host = '@cluesurf/kink'
32
+
33
+ type Base = {
34
+ syntax_error: {}
35
+ }
36
+
37
+ type Name = keyof Base
38
+
39
+ Kink.base(host, 'syntax_error', () => ({
40
+ code: 1,
41
+ note: 'Syntax error',
42
+ }))
43
+
44
+ Kink.code(host, (code: number) => code.toString(16).padStart(4, '0'))
45
+
46
+ export default function kink<N extends Name>(form: N, link?: Base[N]) {
47
+ return new Kink(Kink.makeBase(host, form, link))
48
+ }
49
+ ```
50
+
51
+ ```ts
52
+ import kink from './example.js'
53
+
54
+ try {
55
+ throw kink('syntax_error')
56
+ } catch (e) {
57
+ console.log(e)
58
+ }
59
+ ```
60
+
61
+ ## License
62
+
63
+ MIT
64
+
65
+ ## ClueSurf
66
+
67
+ This is being developed by the folks at [ClueSurf](https://clue.surf), a
68
+ California-based project for helping humanity master information and
69
+ computation. Find us on [Twitter](https://twitter.com/cluesurf),
70
+ [LinkedIn](https://www.linkedin.com/company/cluesurf), and
71
+ [Facebook](https://www.facebook.com/cluesurf). Check out our other
72
+ [GitHub projects](https://github.com/cluesurf) as well!