Add type guard utils to wallet utils
This commit is contained in:
@@ -57,15 +57,18 @@ export const buildBlobUrl = (server: string, sha256: string, extension?: string)
|
|||||||
export const checkBlobExists = async (
|
export const checkBlobExists = async (
|
||||||
server: string,
|
server: string,
|
||||||
sha256: string,
|
sha256: string,
|
||||||
options: {
|
{
|
||||||
|
headers = {},
|
||||||
|
authEvent,
|
||||||
|
}: {
|
||||||
|
headers?: Record<string, string>
|
||||||
authEvent?: SignedEvent
|
authEvent?: SignedEvent
|
||||||
} = {},
|
} = {},
|
||||||
): Promise<{exists: boolean; size?: number}> => {
|
): Promise<{exists: boolean; size?: number}> => {
|
||||||
const url = buildBlobUrl(server, sha256)
|
const url = buildBlobUrl(server, sha256)
|
||||||
const headers: Record<string, string> = {}
|
|
||||||
|
|
||||||
if (options.authEvent) {
|
if (authEvent) {
|
||||||
headers.Authorization = makeHttpAuthHeader(options.authEvent)
|
headers.Authorization = makeHttpAuthHeader(authEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -88,20 +91,24 @@ export const checkBlobExists = async (
|
|||||||
export const getBlob = async (
|
export const getBlob = async (
|
||||||
server: string,
|
server: string,
|
||||||
sha256: string,
|
sha256: string,
|
||||||
options: {
|
{
|
||||||
|
headers = {},
|
||||||
|
authEvent,
|
||||||
|
range,
|
||||||
|
}: {
|
||||||
|
headers?: Record<string, string>
|
||||||
authEvent?: SignedEvent
|
authEvent?: SignedEvent
|
||||||
range?: {start: number; end?: number}
|
range?: {start: number; end?: number}
|
||||||
} = {},
|
} = {},
|
||||||
) => {
|
) => {
|
||||||
const url = buildBlobUrl(server, sha256)
|
const url = buildBlobUrl(server, sha256)
|
||||||
const headers: Record<string, string> = {}
|
|
||||||
|
|
||||||
if (options.authEvent) {
|
if (authEvent) {
|
||||||
headers.Authorization = makeHttpAuthHeader(options.authEvent)
|
headers.Authorization = makeHttpAuthHeader(authEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.range) {
|
if (range) {
|
||||||
const {end, start} = options.range
|
const {end, start} = range
|
||||||
|
|
||||||
headers.Range = end !== undefined ? `bytes=${start}-${end}` : `bytes=${start}-`
|
headers.Range = end !== undefined ? `bytes=${start}-${end}` : `bytes=${start}-`
|
||||||
}
|
}
|
||||||
@@ -109,20 +116,43 @@ export const getBlob = async (
|
|||||||
return fetch(url, {headers})
|
return fetch(url, {headers})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const canUploadBlob = async (
|
||||||
|
server: string,
|
||||||
|
{
|
||||||
|
headers = {},
|
||||||
|
authEvent,
|
||||||
|
}: {
|
||||||
|
headers?: Record<string, string>
|
||||||
|
authEvent?: SignedEvent
|
||||||
|
} = {},
|
||||||
|
) => {
|
||||||
|
const url = new URL(server)
|
||||||
|
const uploadUrl = `${url.origin}/upload`
|
||||||
|
|
||||||
|
if (authEvent) {
|
||||||
|
headers.Authorization = makeHttpAuthHeader(authEvent)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch(uploadUrl, {method: "HEAD", headers})
|
||||||
|
}
|
||||||
|
|
||||||
export const uploadBlob = async (
|
export const uploadBlob = async (
|
||||||
server: string,
|
server: string,
|
||||||
blob: Blob | ArrayBuffer,
|
blob: Blob | ArrayBuffer,
|
||||||
options: {
|
{
|
||||||
|
headers = {},
|
||||||
|
authEvent,
|
||||||
|
}: {
|
||||||
|
headers?: Record<string, string>
|
||||||
authEvent?: SignedEvent
|
authEvent?: SignedEvent
|
||||||
} = {},
|
} = {},
|
||||||
) => {
|
) => {
|
||||||
const url = new URL(server)
|
const url = new URL(server)
|
||||||
const uploadUrl = `${url.origin}/upload`
|
const uploadUrl = `${url.origin}/upload`
|
||||||
const body = blob instanceof Blob ? blob : new Blob([blob])
|
const body = blob instanceof Blob ? blob : new Blob([blob])
|
||||||
const headers: Record<string, string> = {}
|
|
||||||
|
|
||||||
if (options.authEvent) {
|
if (authEvent) {
|
||||||
headers.Authorization = makeHttpAuthHeader(options.authEvent)
|
headers.Authorization = makeHttpAuthHeader(authEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(uploadUrl, {method: "PUT", headers, body})
|
return fetch(uploadUrl, {method: "PUT", headers, body})
|
||||||
@@ -131,16 +161,18 @@ export const uploadBlob = async (
|
|||||||
export const deleteBlob = async (
|
export const deleteBlob = async (
|
||||||
server: string,
|
server: string,
|
||||||
sha256: string,
|
sha256: string,
|
||||||
options: {
|
{
|
||||||
|
headers = {},
|
||||||
|
authEvent,
|
||||||
|
}: {
|
||||||
|
headers?: Record<string, string>
|
||||||
authEvent?: SignedEvent
|
authEvent?: SignedEvent
|
||||||
} = {},
|
} = {},
|
||||||
) => {
|
) => {
|
||||||
const url = buildBlobUrl(server, sha256)
|
const url = buildBlobUrl(server, sha256)
|
||||||
|
|
||||||
const headers: Record<string, string> = {}
|
if (authEvent) {
|
||||||
|
headers.Authorization = makeHttpAuthHeader(authEvent)
|
||||||
if (options.authEvent) {
|
|
||||||
headers.Authorization = makeHttpAuthHeader(options.authEvent)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(url, {method: "DELETE", headers})
|
return fetch(url, {method: "DELETE", headers})
|
||||||
@@ -149,7 +181,13 @@ export const deleteBlob = async (
|
|||||||
export const listBlobs = async (
|
export const listBlobs = async (
|
||||||
server: string,
|
server: string,
|
||||||
pubkey: string,
|
pubkey: string,
|
||||||
options: {
|
{
|
||||||
|
headers = {},
|
||||||
|
authEvent,
|
||||||
|
since,
|
||||||
|
until,
|
||||||
|
}: {
|
||||||
|
headers?: Record<string, string>
|
||||||
authEvent?: SignedEvent
|
authEvent?: SignedEvent
|
||||||
since?: number
|
since?: number
|
||||||
until?: number
|
until?: number
|
||||||
@@ -159,19 +197,19 @@ export const listBlobs = async (
|
|||||||
const listUrl = `${url.origin}/list/${pubkey}`
|
const listUrl = `${url.origin}/list/${pubkey}`
|
||||||
|
|
||||||
const searchParams = new URLSearchParams()
|
const searchParams = new URLSearchParams()
|
||||||
if (options.since !== undefined) {
|
|
||||||
searchParams.append("since", options.since.toString())
|
if (since !== undefined) {
|
||||||
|
searchParams.append("since", since.toString())
|
||||||
}
|
}
|
||||||
if (options.until !== undefined) {
|
|
||||||
searchParams.append("until", options.until.toString())
|
if (until !== undefined) {
|
||||||
|
searchParams.append("until", until.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
const fullUrl = searchParams.toString() ? `${listUrl}?${searchParams.toString()}` : listUrl
|
const fullUrl = searchParams.toString() ? `${listUrl}?${searchParams.toString()}` : listUrl
|
||||||
|
|
||||||
const headers: Record<string, string> = {}
|
if (authEvent) {
|
||||||
|
headers.Authorization = makeHttpAuthHeader(authEvent)
|
||||||
if (options.authEvent) {
|
|
||||||
headers.Authorization = makeHttpAuthHeader(options.authEvent)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(fullUrl, {headers})
|
return fetch(fullUrl, {headers})
|
||||||
|
|||||||
+17
-10
@@ -16,16 +16,23 @@ export type NWCInfo = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export enum WalletType {
|
export enum WalletType {
|
||||||
WebLn = "webln",
|
WebLN = "webln",
|
||||||
NWC = "nwc",
|
NWC = "nwc",
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Wallet =
|
export type WebLNWallet = {
|
||||||
| {
|
type: WalletType.WebLN
|
||||||
type: WalletType.WebLn
|
info: WebLNInfo
|
||||||
info: WebLNInfo
|
}
|
||||||
}
|
|
||||||
| {
|
export type NWCWallet = {
|
||||||
type: WalletType.NWC
|
type: WalletType.NWC
|
||||||
info: NWCInfo
|
info: NWCInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Wallet = WebLNWallet | NWCWallet
|
||||||
|
|
||||||
|
export const isWebLNWallet = (wallet: Wallet): wallet is WebLNWallet =>
|
||||||
|
wallet.type === WalletType.WebLN
|
||||||
|
|
||||||
|
export const isNWCWallet = (wallet: Wallet): wallet is NWCWallet => wallet.type === WalletType.NWC
|
||||||
|
|||||||
Reference in New Issue
Block a user