Skip to content

Commit

Permalink
fix(nips/01): fix SubscriptionFilter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Oct 4, 2023
1 parent a57295b commit 2ef24d0
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
22 changes: 12 additions & 10 deletions nips/01.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ export type EventSerializePrecursor<K extends EventKind = EventKind> = [
export type AnyTag = Tag<string>;
export type IndexedTag = Tag<AlphabetLetter>;

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

export interface TagValueFor extends Record<string, TagValue> {
export interface TagContentFor extends Record<string, TagContent> {
// Event
"e": [EventId, RelayUrl?];
// Public key
"p": [PublicKey, RelayUrl?];
// (Maybe parameterized) replaceable event
"a": [
`${EventKind}:${PublicKey}:${TagValueFor["d"][0]}`,
`${EventKind}:${PublicKey}:${TagValueFor["d"]}`,
RelayUrl?,
] | [
`${EventKind}:${PublicKey}`,
Expand All @@ -61,7 +61,11 @@ export interface TagValueFor extends Record<string, TagValue> {
}

// TODO: Tighten the type of TagValue
export type TagValue = (string | undefined)[];
export type TagContent = (string | undefined)[];

export type TagValueFor = {
[T in keyof TagContentFor]: TagContentFor[T][0];
};

export interface TagFor extends Record<number, AnyTag> {
0: AnyTag;
Expand Down Expand Up @@ -101,7 +105,6 @@ export type EventMessage<K extends EventKind = EventKind> = [
];
export type OkMessage<B extends boolean = boolean> = [
"OK",

EventId,
B,
OkMessageBody<B>,
Expand All @@ -123,17 +126,16 @@ export type OkMessageBodyPrefix =
| "error";

export type SubscriptionFilter<
K extends EventKind = EventKind,
T extends AlphabetLetter = AlphabetLetter,
Ks extends EventKind = EventKind,
Ts extends AlphabetLetter = AlphabetLetter,
> =
& {
ids?: EventId[];
authors?: PublicKey[];
kinds?: K[];
kinds?: Ks[];
}
// TODO: Restrict to 1 tag per filter
& {
[key in `#${T}`]?: TagValueFor[T][];
[T in Ts as `#${T}`]?: TagValueFor[T][];
}
& {
since?: Timestamp;
Expand Down
79 changes: 78 additions & 1 deletion nips/01_test.ts
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@
import {} from "./01.ts";
import { describe, it } from "../lib/std/testing.ts";
import { assert } from "../lib/std/assert.ts";
import type { EventId, PublicKey } from "../mod.ts";
import { Timestamp } from "../lib/times.ts";
import { SubscriptionFilter } from "./01.ts";

describe("SubscriptionFilter", () => {
it("valid", () => {
const filter = {
ids: ["" as EventId],
kinds: [0, 1],
authors: ["" as PublicKey],
"#p": ["" as PublicKey],
"#e": ["" as EventId],
"#Z": [""],
since: Timestamp.now,
until: Timestamp.now,
limit: 10,
} satisfies SubscriptionFilter;
assert(filter);
});
it("ids should be an array of EventId", () => {
const filter = {
// @ts-expect-error: ids should be EventId[]
ids: [""],
} satisfies SubscriptionFilter;
assert(filter);
});
it("kinds should be an array of EventKind", () => {
const filter = {
// @ts-expect-error: kinds should be EventKind[]
kinds: [""],
} satisfies SubscriptionFilter;
assert(filter);
});
it("authors should be an array of PublicKey", () => {
const filter = {
// @ts-expect-error: authors should be PublicKey[]
authors: [""],
} satisfies SubscriptionFilter;
assert(filter);
});
it("tag #p should be an array of PublicKey", () => {
const filter = {
// @ts-expect-error: tag #p should be PublicKey[]
"#p": [""],
} satisfies SubscriptionFilter;
assert(filter);
});
it("tag #e should be an array of EventId", () => {
const filter = {
// @ts-expect-error: tag #e should be EventId[]
"#e": [""],
} satisfies SubscriptionFilter;
assert(filter);
});
it("since should be Timestamp", () => {
const filter = {
// @ts-expect-error: since should be Timestamp
since: 0,
} satisfies SubscriptionFilter;
assert(filter);
});
it("until should be Timestamp", () => {
const filter = {
// @ts-expect-error: until should be Timestamp
until: 0,
} satisfies SubscriptionFilter;
assert(filter);
});
it("limit should be number", () => {
const filter = {
// @ts-expect-error: limit should be number
limit: "10",
} satisfies SubscriptionFilter;
assert(filter);
});
});

0 comments on commit 2ef24d0

Please sign in to comment.