Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Leif Shackelford committed Jul 5, 2022
1 parent e1c0984 commit 01b6d7f
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 196 deletions.
4 changes: 2 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { uapiFetch, FetchOptions, getOptions } from "./fetch";
export { FetchOptions };
export interface WellKnown {
endpoint: string;
'path-components'?: string;
"path-components"?: string;
vendor?: string;
example?: {
qr: string;
Expand All @@ -23,7 +23,7 @@ export declare const Endpoints: {
};
};
export declare const getQr: (qr: string, route?: string, options?: FetchOptions) => Promise<any>;
export declare const postQr: (qr: string, route?: string, options?: FetchOptions) => Promise<unknown>;
export declare const postQr: (qr: string, route?: string, options?: FetchOptions) => Promise<any>;
export { postQr as putQr };
export { uapiFetch };
export { getOptions };
Expand Down
38 changes: 16 additions & 22 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 69 additions & 65 deletions src/index.ts
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];
};
Loading

0 comments on commit 01b6d7f

Please sign in to comment.