-
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.
- Loading branch information
Leif Shackelford
committed
Jul 5, 2022
1 parent
e1c0984
commit 01b6d7f
Showing
5 changed files
with
240 additions
and
196 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,84 +1,88 @@ | ||
import { parsePathComponents, applyPathComponents } from "./lib"; | ||
import { uapiFetch, FetchOptions, getOptions } from "./fetch"; | ||
export { FetchOptions } | ||
export { FetchOptions }; | ||
|
||
export interface WellKnown { | ||
endpoint: string // 'https://signet.codes/uapi/v1', | ||
'path-components'?: string // 'https://s.chroma.io/:id', | ||
vendor?: string // 'Chroma' | ||
example?: { | ||
qr: string | ||
} | ||
endpoint: string; // 'https://signet.codes/uapi/v1', | ||
"path-components"?: string; // 'https://s.chroma.io/:id', | ||
vendor?: string; // 'Chroma' | ||
example?: { | ||
qr: string; | ||
}; | ||
} | ||
|
||
const info: { [x: string]: WellKnown } = {}; | ||
|
||
export const Endpoints = { | ||
case: { | ||
get: "case/{:id}", | ||
post: "case/{:id}" | ||
}, | ||
each: { | ||
get: "each/{:id}", | ||
post: "each/{:id}", | ||
}, | ||
regulator: { | ||
get: "regulator/{:id}", | ||
post: "regulator/{:id}" | ||
} | ||
} | ||
|
||
export const getQr = async (qr: string, route = "case", options?: FetchOptions) => { | ||
const { endpoint, ["path-components"]: pathComponents } = await fetchEndpointInfo(qr); | ||
let vars = {}; | ||
const qrPathDefinition = pathComponents; | ||
let uri = endpoint; | ||
if (qrPathDefinition) { | ||
vars = parsePathComponents(qr, qrPathDefinition); | ||
const endpointPathDefinition = `${endpoint}/${Endpoints[route].get}`; | ||
uri = applyPathComponents(endpointPathDefinition, vars); | ||
} | ||
const res = await uapiFetch({ uri, ...options }); | ||
return await res.json(); | ||
case: { | ||
get: "case/{:id}", | ||
post: "case/{:id}", | ||
}, | ||
each: { | ||
get: "each/{:id}", | ||
post: "each/{:id}", | ||
}, | ||
regulator: { | ||
get: "regulator/{:id}", | ||
post: "regulator/{:id}", | ||
}, | ||
}; | ||
|
||
} | ||
export const getQr = async ( | ||
qr: string, | ||
route = "case", | ||
options?: FetchOptions | ||
) => { | ||
const { endpoint, ["path-components"]: pathComponents } = | ||
await fetchEndpointInfo(qr); | ||
let vars = {}; | ||
const qrPathDefinition = pathComponents; | ||
let uri = endpoint; | ||
if (qrPathDefinition) { | ||
vars = parsePathComponents(qr, qrPathDefinition); | ||
const endpointPathDefinition = `${endpoint}/${Endpoints[route].get}`; | ||
uri = applyPathComponents(endpointPathDefinition, vars); | ||
} | ||
const res = await uapiFetch({ uri, ...options }); | ||
return await res.json(); | ||
}; | ||
|
||
export const postQr = (qr: string, route = "case", options?: FetchOptions) => { | ||
// if (!apiKey) { | ||
// throw new Error("please provide a JWT string to be encoded as a header"); | ||
// } | ||
return new Promise(async (resolve, reject) => { | ||
const url = new URL(qr); | ||
const epInfo = await fetchEndpointInfo(qr); | ||
|
||
let vars = {}; | ||
const qrPathDefinition = epInfo['path-components']; | ||
let uri = epInfo.endpoint; | ||
if (qrPathDefinition) { | ||
vars = parsePathComponents(qr, qrPathDefinition); | ||
console.log("apply path vars", vars) | ||
const endpointPathDefinition = `${epInfo.endpoint}/${Endpoints[route].get}`; | ||
uri = applyPathComponents(endpointPathDefinition, vars); | ||
} | ||
const res = await uapiFetch({ uri, ...options }); | ||
return await res.json(); | ||
}) | ||
} | ||
export const postQr = async ( | ||
qr: string, | ||
route = "case", | ||
options?: FetchOptions | ||
) => { | ||
const { endpoint, ["path-components"]: pathComponents } = | ||
await fetchEndpointInfo(qr); | ||
let vars = {}; | ||
const qrPathDefinition = pathComponents; | ||
let uri = endpoint; | ||
if (qrPathDefinition) { | ||
vars = parsePathComponents(qr, qrPathDefinition); | ||
const endpointPathDefinition = `${endpoint}/${Endpoints[route].get}`; | ||
uri = applyPathComponents(endpointPathDefinition, vars); | ||
} | ||
const res = await uapiFetch({ uri, method: "POST", ...options }); | ||
return await res.json(); | ||
}; | ||
|
||
export { postQr as putQr }; | ||
|
||
export { uapiFetch }; | ||
export { getOptions }; | ||
|
||
export const fetchEndpointInfo = async (qr: string, apiKey?: string): Promise<WellKnown> => { | ||
const url = new URL(qr); | ||
if (info[url.host] !== undefined) return info[url.host]; | ||
export const fetchEndpointInfo = async ( | ||
qr: string, | ||
apiKey?: string | ||
): Promise<WellKnown> => { | ||
const url = new URL(qr); | ||
if (info[url.host] !== undefined) return info[url.host]; | ||
|
||
const endpoint = `https://${url.host}`; | ||
const endpointInfo = `${endpoint}/.well-known/cannabis-api.json`; | ||
const res = await fetch(endpointInfo); | ||
const body = await res.json(); | ||
const endpoint = `https://${url.host}`; | ||
const endpointInfo = `${endpoint}/.well-known/cannabis-api.json`; | ||
const res = await fetch(endpointInfo); | ||
const body = await res.json(); | ||
|
||
info[url.host] = body; | ||
return info[url.host]; | ||
} | ||
info[url.host] = body; | ||
return info[url.host]; | ||
}; |
Oops, something went wrong.