mirror of
https://github.com/Blah-IM/typescript-core.git
synced 2025-04-30 08:11:10 +00:00
refactor: spell out all types & test types against schema
This commit is contained in:
parent
51f7a3cdd1
commit
a2ef1311a4
15 changed files with 154 additions and 25 deletions
|
@ -13,10 +13,10 @@ export class BlahKeyPair {
|
|||
publicKey: BlahPublicKey;
|
||||
private privateKey: CryptoKey;
|
||||
|
||||
get id() {
|
||||
get id(): string {
|
||||
return this.publicKey.id;
|
||||
}
|
||||
get name() {
|
||||
get name(): string {
|
||||
return this.publicKey.name;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
export * from "./keypair.ts";
|
||||
export * from "./publicKey.ts";
|
||||
export * from "./signedPayload.ts";
|
||||
export type { BlahPayloadSignee, BlahSignedPayload } from "./signedPayload.ts";
|
||||
export * from "./utils.ts";
|
||||
|
|
25
crypto/signedPayload.test.ts
Normal file
25
crypto/signedPayload.test.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import z from "zod";
|
||||
import type { BlahSignedPayload } from "./mod.ts";
|
||||
import {
|
||||
type BlahPayloadSignee,
|
||||
blahPayloadSigneeSchemaOf,
|
||||
blahSignedPayloadSchemaOf,
|
||||
} from "./signedPayload.ts";
|
||||
import { assertTypeMatchesZodSchema } from "../test/utils.ts";
|
||||
|
||||
const testPayloadSchema = z.object({
|
||||
foo: z.string(),
|
||||
});
|
||||
type TestPayload = z.infer<typeof testPayloadSchema>;
|
||||
|
||||
Deno.test("type BlahPayloadSignee is accurate", () => {
|
||||
assertTypeMatchesZodSchema<BlahPayloadSignee<TestPayload>>(
|
||||
blahPayloadSigneeSchemaOf(testPayloadSchema),
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("type BlahSignedPayload is accurate", () => {
|
||||
assertTypeMatchesZodSchema<BlahSignedPayload<TestPayload>>(
|
||||
blahSignedPayloadSchemaOf(testPayloadSchema),
|
||||
);
|
||||
});
|
|
@ -17,10 +17,15 @@ export function blahSignedPayloadSchemaOf<P extends z.ZodTypeAny>(schema: P) {
|
|||
});
|
||||
}
|
||||
|
||||
export type BlahPayloadSignee<P extends z.ZodTypeAny> = z.infer<
|
||||
ReturnType<typeof blahPayloadSigneeSchemaOf<P>>
|
||||
>;
|
||||
export type BlahPayloadSignee<P> = {
|
||||
nonce: number;
|
||||
payload: P;
|
||||
timestamp: number;
|
||||
id_key: string;
|
||||
act_key?: string;
|
||||
};
|
||||
|
||||
export type BlahSignedPayload<P extends z.ZodTypeAny> = z.infer<
|
||||
ReturnType<typeof blahSignedPayloadSchemaOf<P>>
|
||||
>;
|
||||
export type BlahSignedPayload<P> = {
|
||||
sig: string;
|
||||
signee: BlahPayloadSignee<P>;
|
||||
};
|
||||
|
|
|
@ -8,6 +8,10 @@
|
|||
"imports": {
|
||||
"@deno/dnt": "jsr:@deno/dnt@^0.41.3",
|
||||
"@std/expect": "jsr:@std/expect@^1.0.3",
|
||||
"@std/testing": "jsr:@std/testing@^1.0.3",
|
||||
"zod": "https://deno.land/x/zod@v3.23.8/mod.ts"
|
||||
},
|
||||
"tasks": {
|
||||
"build:npm": "deno run ./scripts/build_npm.ts"
|
||||
}
|
||||
}
|
||||
|
|
48
deno.lock
generated
48
deno.lock
generated
|
@ -9,20 +9,26 @@
|
|||
"jsr:@std/assert@^0.223.0": "jsr:@std/assert@0.223.0",
|
||||
"jsr:@std/assert@^0.226.0": "jsr:@std/assert@0.226.0",
|
||||
"jsr:@std/assert@^1.0.5": "jsr:@std/assert@1.0.5",
|
||||
"jsr:@std/assert@^1.0.6": "jsr:@std/assert@1.0.6",
|
||||
"jsr:@std/bytes@^0.223.0": "jsr:@std/bytes@0.223.0",
|
||||
"jsr:@std/expect@^1.0.3": "jsr:@std/expect@1.0.3",
|
||||
"jsr:@std/data-structures@^1.0.4": "jsr:@std/data-structures@1.0.4",
|
||||
"jsr:@std/expect@^1.0.3": "jsr:@std/expect@1.0.4",
|
||||
"jsr:@std/fmt@1": "jsr:@std/fmt@1.0.2",
|
||||
"jsr:@std/fmt@^0.223": "jsr:@std/fmt@0.223.0",
|
||||
"jsr:@std/fs@1": "jsr:@std/fs@1.0.3",
|
||||
"jsr:@std/fs@1": "jsr:@std/fs@1.0.4",
|
||||
"jsr:@std/fs@^0.223": "jsr:@std/fs@0.223.0",
|
||||
"jsr:@std/fs@^0.229.3": "jsr:@std/fs@0.229.3",
|
||||
"jsr:@std/fs@^1.0.4": "jsr:@std/fs@1.0.4",
|
||||
"jsr:@std/internal@^1.0.3": "jsr:@std/internal@1.0.3",
|
||||
"jsr:@std/internal@^1.0.4": "jsr:@std/internal@1.0.4",
|
||||
"jsr:@std/io@^0.223": "jsr:@std/io@0.223.0",
|
||||
"jsr:@std/path@1": "jsr:@std/path@1.0.6",
|
||||
"jsr:@std/path@1.0.0-rc.1": "jsr:@std/path@1.0.0-rc.1",
|
||||
"jsr:@std/path@^0.223": "jsr:@std/path@0.223.0",
|
||||
"jsr:@std/path@^0.225.2": "jsr:@std/path@0.225.2",
|
||||
"jsr:@std/path@^1.0.4": "jsr:@std/path@1.0.6",
|
||||
"jsr:@std/path@^1.0.6": "jsr:@std/path@1.0.6",
|
||||
"jsr:@std/testing@^1.0.3": "jsr:@std/testing@1.0.3",
|
||||
"jsr:@ts-morph/bootstrap@^0.24.0": "jsr:@ts-morph/bootstrap@0.24.0",
|
||||
"jsr:@ts-morph/common@^0.24.0": "jsr:@ts-morph/common@0.24.0"
|
||||
},
|
||||
|
@ -66,9 +72,18 @@
|
|||
"jsr:@std/internal@^1.0.3"
|
||||
]
|
||||
},
|
||||
"@std/assert@1.0.6": {
|
||||
"integrity": "1904c05806a25d94fe791d6d883b685c9e2dcd60e4f9fc30f4fc5cf010c72207",
|
||||
"dependencies": [
|
||||
"jsr:@std/internal@^1.0.4"
|
||||
]
|
||||
},
|
||||
"@std/bytes@0.223.0": {
|
||||
"integrity": "84b75052cd8680942c397c2631318772b295019098f40aac5c36cead4cba51a8"
|
||||
},
|
||||
"@std/data-structures@1.0.4": {
|
||||
"integrity": "fa0e20c11eb9ba673417450915c750a0001405a784e2a4e0c3725031681684a0"
|
||||
},
|
||||
"@std/expect@1.0.3": {
|
||||
"integrity": "d9cbd03323ef7feafd1e969ed85d5edb04ebbd9937b0fe7a52d5ff53be8e913a",
|
||||
"dependencies": [
|
||||
|
@ -76,6 +91,13 @@
|
|||
"jsr:@std/internal@^1.0.3"
|
||||
]
|
||||
},
|
||||
"@std/expect@1.0.4": {
|
||||
"integrity": "97f68a445a9de0d9670200d2b7a19a7505a01b2cb390a983ba8d97d90ce30c4f",
|
||||
"dependencies": [
|
||||
"jsr:@std/assert@^1.0.6",
|
||||
"jsr:@std/internal@^1.0.4"
|
||||
]
|
||||
},
|
||||
"@std/fmt@0.223.0": {
|
||||
"integrity": "6deb37794127dfc7d7bded2586b9fc6f5d50e62a8134846608baf71ffc1a5208"
|
||||
},
|
||||
|
@ -97,9 +119,18 @@
|
|||
"jsr:@std/path@^1.0.4"
|
||||
]
|
||||
},
|
||||
"@std/fs@1.0.4": {
|
||||
"integrity": "2907d32d8d1d9e540588fd5fe0ec21ee638134bd51df327ad4e443aaef07123c",
|
||||
"dependencies": [
|
||||
"jsr:@std/path@^1.0.6"
|
||||
]
|
||||
},
|
||||
"@std/internal@1.0.3": {
|
||||
"integrity": "208e9b94a3d5649bd880e9ca38b885ab7651ab5b5303a56ed25de4755fb7b11e"
|
||||
},
|
||||
"@std/internal@1.0.4": {
|
||||
"integrity": "62e8e4911527e5e4f307741a795c0b0a9e6958d0b3790716ae71ce085f755422"
|
||||
},
|
||||
"@std/io@0.223.0": {
|
||||
"integrity": "2d8c3c2ab3a515619b90da2c6ff5ea7b75a94383259ef4d02116b228393f84f1",
|
||||
"dependencies": [
|
||||
|
@ -125,6 +156,16 @@
|
|||
"@std/path@1.0.6": {
|
||||
"integrity": "ab2c55f902b380cf28e0eec501b4906e4c1960d13f00e11cfbcd21de15f18fed"
|
||||
},
|
||||
"@std/testing@1.0.3": {
|
||||
"integrity": "f98c2bee53860a5916727d7e7d3abe920dd6f9edace022e2d059f00d05c2cf42",
|
||||
"dependencies": [
|
||||
"jsr:@std/assert@^1.0.6",
|
||||
"jsr:@std/data-structures@^1.0.4",
|
||||
"jsr:@std/fs@^1.0.4",
|
||||
"jsr:@std/internal@^1.0.4",
|
||||
"jsr:@std/path@^1.0.6"
|
||||
]
|
||||
},
|
||||
"@ts-morph/bootstrap@0.24.0": {
|
||||
"integrity": "a826a2ef7fa8a7c3f1042df2c034d20744d94da2ee32bf29275bcd4dffd3c060",
|
||||
"dependencies": [
|
||||
|
@ -158,7 +199,8 @@
|
|||
"workspace": {
|
||||
"dependencies": [
|
||||
"jsr:@deno/dnt@^0.41.3",
|
||||
"jsr:@std/expect@^1.0.3"
|
||||
"jsr:@std/expect@^1.0.3",
|
||||
"jsr:@std/testing@^1.0.3"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
6
identity/actKey.test.ts
Normal file
6
identity/actKey.test.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { type BlahActKeyRecord, blahActKeyRecordSchema } from "./actKey.ts";
|
||||
import { assertTypeMatchesZodSchema } from "../test/utils.ts";
|
||||
|
||||
Deno.test("type BlahActKeyRecord is accurate", () => {
|
||||
assertTypeMatchesZodSchema<BlahActKeyRecord>(blahActKeyRecordSchema);
|
||||
});
|
|
@ -1,11 +1,15 @@
|
|||
import z from "zod";
|
||||
|
||||
export const blahActKeyRecordSchema = z.object({
|
||||
typ: z.literal("use_act_key"),
|
||||
typ: z.literal("user_act_key"),
|
||||
act_key: z.string(),
|
||||
expire_time: z.number().int(),
|
||||
comment: z.string(),
|
||||
});
|
||||
|
||||
export type BlahActKeyRecord = z.input<typeof blahActKeyRecordSchema>;
|
||||
export type BlahParsedActKeyRecord = z.infer<typeof blahActKeyRecordSchema>;
|
||||
export type BlahActKeyRecord = {
|
||||
typ: "user_act_key";
|
||||
act_key: string;
|
||||
expire_time: number;
|
||||
comment: string;
|
||||
};
|
||||
|
|
9
identity/identity.ts
Normal file
9
identity/identity.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import type { BlahSignedPayload } from "../crypto/mod.ts";
|
||||
import type { BlahActKeyRecord } from "./actKey.ts";
|
||||
import type { BlahProfile } from "./profile.ts";
|
||||
|
||||
export class BlahIdentity {
|
||||
idKeyId: string;
|
||||
actKeys: BlahSignedPayload<BlahActKeyRecord>[];
|
||||
profile: BlahSignedPayload<BlahProfile>;
|
||||
}
|
9
identity/identityFile.test.ts
Normal file
9
identity/identityFile.test.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import {
|
||||
type BlahIdentityFile,
|
||||
blahIdentityFileSchema,
|
||||
} from "./identityFile.ts";
|
||||
import { assertTypeMatchesZodSchema } from "../test/utils.ts";
|
||||
|
||||
Deno.test("type BlahIdentityFile is accurate", () => {
|
||||
assertTypeMatchesZodSchema<BlahIdentityFile>(blahIdentityFileSchema);
|
||||
});
|
|
@ -1,7 +1,8 @@
|
|||
import { z } from "zod";
|
||||
import { blahSignedPayloadSchemaOf } from "../crypto/mod.ts";
|
||||
import { blahActKeyRecordSchema } from "./actKey.ts";
|
||||
import { blahProfileSchema } from "./profile.ts";
|
||||
import { blahSignedPayloadSchemaOf } from "../crypto/signedPayload.ts";
|
||||
import { type BlahActKeyRecord, blahActKeyRecordSchema } from "./actKey.ts";
|
||||
import { type BlahProfile, blahProfileSchema } from "./profile.ts";
|
||||
import type { BlahSignedPayload } from "../crypto/mod.ts";
|
||||
|
||||
export const blahIdentityFileSchema = z.object({
|
||||
id_key: z.string(),
|
||||
|
@ -9,5 +10,8 @@ export const blahIdentityFileSchema = z.object({
|
|||
profile: blahSignedPayloadSchemaOf(blahProfileSchema),
|
||||
});
|
||||
|
||||
export type BlahIdentityFile = z.input<typeof blahIdentityFileSchema>;
|
||||
export type BlahParsedIdentityFile = z.infer<typeof blahIdentityFileSchema>;
|
||||
export type BlahIdentityFile = {
|
||||
id_key: string;
|
||||
act_keys: Array<BlahSignedPayload<BlahActKeyRecord>>;
|
||||
profile: BlahSignedPayload<BlahProfile>;
|
||||
};
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
export * from "./actKey.ts";
|
||||
export * from "./identityFile.ts";
|
||||
export * from "./profile.ts";
|
||||
export type { BlahActKeyRecord } from "./actKey.ts";
|
||||
export type { BlahIdentityFile } from "./identityFile.ts";
|
||||
export type { BlahProfile } from "./profile.ts";
|
||||
|
|
6
identity/profile.test.ts
Normal file
6
identity/profile.test.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { type BlahProfile, blahProfileSchema } from "./profile.ts";
|
||||
import { assertTypeMatchesZodSchema } from "../test/utils.ts";
|
||||
|
||||
Deno.test("type BlahProfile is accurate", () => {
|
||||
assertTypeMatchesZodSchema<BlahProfile>(blahProfileSchema);
|
||||
});
|
|
@ -8,5 +8,10 @@ export const blahProfileSchema = z.object({
|
|||
bio: z.string().optional(),
|
||||
});
|
||||
|
||||
export type BlahProfile = z.input<typeof blahProfileSchema>;
|
||||
export type BlahParsedProfile = z.infer<typeof blahProfileSchema>;
|
||||
export type BlahProfile = {
|
||||
typ: "profile";
|
||||
preferred_chat_server_urls: string[];
|
||||
id_urls: string[];
|
||||
name: string;
|
||||
bio?: string;
|
||||
};
|
||||
|
|
10
test/utils.ts
Normal file
10
test/utils.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { assertType, type IsExact } from "@std/testing/types";
|
||||
import type { z } from "zod";
|
||||
|
||||
export function assertTypeMatchesZodSchema<T>(
|
||||
schema: z.ZodTypeAny,
|
||||
) {
|
||||
assertType<IsExact<T, z.infer<typeof schema>>>(
|
||||
true as IsExact<T, z.infer<typeof schema>>,
|
||||
);
|
||||
}
|
Loading…
Add table
Reference in a new issue