-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: abandon enums for NIPs and event kinds in favor of type rec…
…ords (#18)
- Loading branch information
Showing
18 changed files
with
117 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { NIP } from "./protocol.d.ts"; | ||
import { NostrNodeModule } from "./nodes.ts"; | ||
|
||
export const NIPs = { | ||
/** | ||
* Import a Nostr module from a URL. | ||
* | ||
* @param meta - The path to the module to which the NIPs are attached (mostly import.meta.url). | ||
* @param root - The path to the root of NIP module to import. | ||
*/ | ||
// deno-lint-ignore no-explicit-any | ||
import<M extends NostrNodeModule<any>>( | ||
meta: string, | ||
root: string, | ||
) { | ||
const url = new URL(meta); | ||
const base = url.pathname.split("/").slice(-1)[0]; | ||
return Promise.all( | ||
url.searchParams.get("nips")?.split(",").map(Number).map( | ||
(nip) => | ||
import( | ||
new URL( | ||
`${root}/${nipToString(nip)}/${base}`, | ||
import.meta.url, | ||
).href | ||
) as Promise<M>, | ||
) ?? [], | ||
); | ||
}, | ||
}; | ||
|
||
/** | ||
* Convert a NIP to a string. If the NIP is less than 10, a leading zero is | ||
* added. | ||
*/ | ||
function nipToString(nip: NIP | number) { | ||
return nip > 9 ? nip.toString() : "0" + nip.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import "../../core/protocol.d.ts"; | ||
|
||
declare module "../../core/protocol.d.ts" { | ||
enum NIP { | ||
ContactList = 2, | ||
} | ||
enum EventKind { | ||
ContactList = 3, | ||
interface NipRecord { | ||
2: { | ||
Tag: "p"; | ||
}; | ||
} | ||
interface EventKindRecord { | ||
3: { | ||
Tag: ["p", PublicKey, RelayUrl, string]; | ||
Tag: ContactTag; | ||
Content: ""; | ||
}; | ||
} | ||
type ContactTag = ["p", PublicKey, RelayUrl, petname: string]; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters