Skip to content

Commit 2ef24d0

Browse files
committed
fix(nips/01): fix SubscriptionFilter interface
1 parent a57295b commit 2ef24d0

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed

nips/01.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ export type EventSerializePrecursor<K extends EventKind = EventKind> = [
4141
export type AnyTag = Tag<string>;
4242
export type IndexedTag = Tag<AlphabetLetter>;
4343

44-
export type Tag<T extends string> = [T, ...TagValueFor[T]];
44+
export type Tag<T extends string> = [T, ...TagContentFor[T]];
4545

46-
export interface TagValueFor extends Record<string, TagValue> {
46+
export interface TagContentFor extends Record<string, TagContent> {
4747
// Event
4848
"e": [EventId, RelayUrl?];
4949
// Public key
5050
"p": [PublicKey, RelayUrl?];
5151
// (Maybe parameterized) replaceable event
5252
"a": [
53-
`${EventKind}:${PublicKey}:${TagValueFor["d"][0]}`,
53+
`${EventKind}:${PublicKey}:${TagValueFor["d"]}`,
5454
RelayUrl?,
5555
] | [
5656
`${EventKind}:${PublicKey}`,
@@ -61,7 +61,11 @@ export interface TagValueFor extends Record<string, TagValue> {
6161
}
6262

6363
// TODO: Tighten the type of TagValue
64-
export type TagValue = (string | undefined)[];
64+
export type TagContent = (string | undefined)[];
65+
66+
export type TagValueFor = {
67+
[T in keyof TagContentFor]: TagContentFor[T][0];
68+
};
6569

6670
export interface TagFor extends Record<number, AnyTag> {
6771
0: AnyTag;
@@ -101,7 +105,6 @@ export type EventMessage<K extends EventKind = EventKind> = [
101105
];
102106
export type OkMessage<B extends boolean = boolean> = [
103107
"OK",
104-
105108
EventId,
106109
B,
107110
OkMessageBody<B>,
@@ -123,17 +126,16 @@ export type OkMessageBodyPrefix =
123126
| "error";
124127

125128
export type SubscriptionFilter<
126-
K extends EventKind = EventKind,
127-
T extends AlphabetLetter = AlphabetLetter,
129+
Ks extends EventKind = EventKind,
130+
Ts extends AlphabetLetter = AlphabetLetter,
128131
> =
129132
& {
130133
ids?: EventId[];
131134
authors?: PublicKey[];
132-
kinds?: K[];
135+
kinds?: Ks[];
133136
}
134-
// TODO: Restrict to 1 tag per filter
135137
& {
136-
[key in `#${T}`]?: TagValueFor[T][];
138+
[T in Ts as `#${T}`]?: TagValueFor[T][];
137139
}
138140
& {
139141
since?: Timestamp;

nips/01_test.ts

+78-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,78 @@
1-
import {} from "./01.ts";
1+
import { describe, it } from "../lib/std/testing.ts";
2+
import { assert } from "../lib/std/assert.ts";
3+
import type { EventId, PublicKey } from "../mod.ts";
4+
import { Timestamp } from "../lib/times.ts";
5+
import { SubscriptionFilter } from "./01.ts";
6+
7+
describe("SubscriptionFilter", () => {
8+
it("valid", () => {
9+
const filter = {
10+
ids: ["" as EventId],
11+
kinds: [0, 1],
12+
authors: ["" as PublicKey],
13+
"#p": ["" as PublicKey],
14+
"#e": ["" as EventId],
15+
"#Z": [""],
16+
since: Timestamp.now,
17+
until: Timestamp.now,
18+
limit: 10,
19+
} satisfies SubscriptionFilter;
20+
assert(filter);
21+
});
22+
it("ids should be an array of EventId", () => {
23+
const filter = {
24+
// @ts-expect-error: ids should be EventId[]
25+
ids: [""],
26+
} satisfies SubscriptionFilter;
27+
assert(filter);
28+
});
29+
it("kinds should be an array of EventKind", () => {
30+
const filter = {
31+
// @ts-expect-error: kinds should be EventKind[]
32+
kinds: [""],
33+
} satisfies SubscriptionFilter;
34+
assert(filter);
35+
});
36+
it("authors should be an array of PublicKey", () => {
37+
const filter = {
38+
// @ts-expect-error: authors should be PublicKey[]
39+
authors: [""],
40+
} satisfies SubscriptionFilter;
41+
assert(filter);
42+
});
43+
it("tag #p should be an array of PublicKey", () => {
44+
const filter = {
45+
// @ts-expect-error: tag #p should be PublicKey[]
46+
"#p": [""],
47+
} satisfies SubscriptionFilter;
48+
assert(filter);
49+
});
50+
it("tag #e should be an array of EventId", () => {
51+
const filter = {
52+
// @ts-expect-error: tag #e should be EventId[]
53+
"#e": [""],
54+
} satisfies SubscriptionFilter;
55+
assert(filter);
56+
});
57+
it("since should be Timestamp", () => {
58+
const filter = {
59+
// @ts-expect-error: since should be Timestamp
60+
since: 0,
61+
} satisfies SubscriptionFilter;
62+
assert(filter);
63+
});
64+
it("until should be Timestamp", () => {
65+
const filter = {
66+
// @ts-expect-error: until should be Timestamp
67+
until: 0,
68+
} satisfies SubscriptionFilter;
69+
assert(filter);
70+
});
71+
it("limit should be number", () => {
72+
const filter = {
73+
// @ts-expect-error: limit should be number
74+
limit: "10",
75+
} satisfies SubscriptionFilter;
76+
assert(filter);
77+
});
78+
});

0 commit comments

Comments
 (0)