cleanup nip-11.
This commit is contained in:
parent
3d767beeb9
commit
39cfc5c09e
|
@ -1,27 +1,15 @@
|
|||
import {Nip11} from './nip11'
|
||||
const requestRelayInfos = Nip11.requestRelayInfos
|
||||
import fetch from 'node-fetch'
|
||||
import { useFetchImplementation, fetchRelayInformation } from './nip11'
|
||||
|
||||
describe('requesting Relay infos as for NIP11', () => {
|
||||
test('testing damus relay', async () => {
|
||||
const expected_relay_name = 'relay.nostr.nu'
|
||||
const expected_relay_description =
|
||||
'A nostr relay build by Edward Hollander.'
|
||||
const expected_supported_nips = [
|
||||
1, 2, 4, 9, 11, 12, 15, 16, 20, 22, 26, 28, 33, 40
|
||||
]
|
||||
describe('requesting relay as for NIP11', () => {
|
||||
useFetchImplementation(fetch)
|
||||
|
||||
const test_relay = 'https://relay.nostr.nu'
|
||||
const relay_infos = await requestRelayInfos(test_relay)
|
||||
const relay_name = relay_infos.name
|
||||
const relay_description = relay_infos.description
|
||||
const fees = relay_infos.fees
|
||||
const admission = fees?.admission
|
||||
const supported_nips = relay_infos.supported_nips
|
||||
const admission_condition = Array.isArray(admission)
|
||||
expect(relay_name).toBe(expected_relay_name)
|
||||
expect(relay_description).toBe(expected_relay_description)
|
||||
expect(fees).toBeTruthy()
|
||||
expect(admission_condition).toBeTruthy()
|
||||
expect(supported_nips).toMatchObject(expected_supported_nips)
|
||||
test('testing a relay', async () => {
|
||||
const info = await fetchRelayInformation('wss://atlas.nostr.land')
|
||||
expect(info.name).toEqual('nostr.land')
|
||||
expect(info.description).toEqual('nostr.land family of relays (us-or-01)')
|
||||
expect(info.fees).toBeTruthy()
|
||||
expect(info.supported_nips).toEqual([1, 2, 4, 9, 11, 12, 16, 20, 22, 28, 33, 40])
|
||||
expect(info.software).toEqual('custom')
|
||||
})
|
||||
})
|
||||
|
|
71
nip11.ts
71
nip11.ts
|
@ -1,39 +1,19 @@
|
|||
// #85 I created an implementation of each of the different
|
||||
// types described in the NIP11.
|
||||
import fetch from 'node-fetch'
|
||||
var _fetch: any
|
||||
|
||||
export namespace Nip11 {
|
||||
export interface requestRelayInfos<
|
||||
N extends string[],
|
||||
A extends boolean,
|
||||
P extends boolean
|
||||
> {
|
||||
(relay_addr: string): Promise<RelayInfos<N, A, P>>
|
||||
}
|
||||
try {
|
||||
_fetch = fetch
|
||||
} catch {}
|
||||
|
||||
// I wanted to use an enum, but eslint is giving me
|
||||
// problems!
|
||||
export function useFetchImplementation(fetchImplementation: any) {
|
||||
_fetch = fetchImplementation
|
||||
}
|
||||
|
||||
// export enum headers_accept {
|
||||
// nostr_json = 'application/nostr+json'
|
||||
// }
|
||||
|
||||
export const requestRelayInfos: requestRelayInfos<any, any, any> = (
|
||||
relay_addr: string
|
||||
) => {
|
||||
return new Promise(async (res, rej) => {
|
||||
try {
|
||||
const accept = 'application/nostr+json'
|
||||
const init = {headers: {accept}}
|
||||
const response = await fetch(relay_addr, init)
|
||||
const relayInfos: RelayInfosTemplate<any> =
|
||||
(await response.json()) as RelayInfosTemplate<any>
|
||||
res(relayInfos)
|
||||
} catch (error) {
|
||||
rej(error)
|
||||
}
|
||||
export async function fetchRelayInformation(url: string) {
|
||||
return (await (
|
||||
await fetch(url.replace('ws://', 'http://').replace('wss://', 'https://'), {
|
||||
headers: { Accept: 'application/nostr+json' },
|
||||
})
|
||||
}
|
||||
).json()) as RelayInformation
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -60,13 +40,13 @@ export namespace Nip11 {
|
|||
* @param software identifying relay software URL
|
||||
* @param version string version identifier
|
||||
*/
|
||||
export interface RelayInfosTemplate<N extends string[]> {
|
||||
export interface BasicRelayInformation {
|
||||
// string identifying relay
|
||||
name: string
|
||||
description: string
|
||||
pubkey: string
|
||||
contact: string
|
||||
supported_nips: N
|
||||
supported_nips: number[]
|
||||
software: string
|
||||
version: string
|
||||
// limitation?: Limitations<A, P>
|
||||
|
@ -124,7 +104,7 @@ export interface RelayInfosTemplate<N extends string[]> {
|
|||
* @param payment_required this relay requires payment
|
||||
* before a new connection may perform any action.
|
||||
*/
|
||||
export interface Limitations<A extends boolean, P extends boolean> {
|
||||
export interface Limitations {
|
||||
max_message_length: number
|
||||
max_subscription: number
|
||||
max_filters: number
|
||||
|
@ -134,19 +114,16 @@ export interface Limitations<A extends boolean, P extends boolean> {
|
|||
max_event_tags: number
|
||||
max_content_length: number
|
||||
min_pow_difficulty: number
|
||||
auth_required: A
|
||||
payment_required: P
|
||||
auth_required: boolean
|
||||
payment_required: boolean
|
||||
}
|
||||
|
||||
type range<L extends number, H extends number> = [L, H]
|
||||
type anyRange = range<any, any>
|
||||
type genericKinds = (number | anyRange)[]
|
||||
interface RetentionDetails<K extends genericKinds> {
|
||||
kinds: K
|
||||
interface RetentionDetails {
|
||||
kinds: (number | number[])[]
|
||||
time?: number | null
|
||||
count?: number | null
|
||||
}
|
||||
type AnyRetentionDetails = RetentionDetails<any>
|
||||
type AnyRetentionDetails = RetentionDetails
|
||||
/**
|
||||
* ### Event Retention
|
||||
|
||||
|
@ -300,13 +277,9 @@ export interface Icon {
|
|||
icon: string
|
||||
}
|
||||
|
||||
export type RelayInfos<
|
||||
N extends string[],
|
||||
A extends boolean,
|
||||
P extends boolean
|
||||
> = RelayInfosTemplate<N> &
|
||||
export type RelayInformation = BasicRelayInformation &
|
||||
Partial<Retention> & {
|
||||
limitation?: Partial<Limitations<A, P>>
|
||||
limitation?: Partial<Limitations>
|
||||
} & Partial<ContentLimitations> &
|
||||
Partial<CommunityPreferences> &
|
||||
Partial<PayToRelay> &
|
||||
|
|
Loading…
Reference in New Issue