@coffre/client 0.6.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/credential.d.ts +24 -0
- package/dist/credential.js +26 -0
- package/dist/{index-M5Gp4kSA.d.ts → index-DgII_R1y.d.ts} +425 -15
- package/dist/index.d.ts +2 -2
- package/dist/index.js +31 -1
- package/dist/routes.d.ts +1 -1
- package/dist/routes.js +6 -0
- package/package.json +6 -1
- package/src/api.ts +294 -15
- package/src/credential.ts +48 -0
- package/src/index.ts +40 -2
- package/src/routes.ts +6 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/credential.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A credential through coffre's credential proxy, for an app `coffre run`
|
|
4
|
+
* started with stand-ins (docs/design/credential-proxy.md): where its
|
|
5
|
+
* client takes no base URL to point at the proxy, or the code is new.
|
|
6
|
+
*
|
|
7
|
+
* import { credential } from '@coffre/client/credential';
|
|
8
|
+
* const schumann = credential('schumann');
|
|
9
|
+
* const companies = await (await schumann.fetch('/companies?q=acme')).json();
|
|
10
|
+
*
|
|
11
|
+
* `coffre run` sets `COFFRE_CREDENTIALS`, each credential's URL on the
|
|
12
|
+
* proxy by name, and `COFFRE_HANDLE`, the handle that stands in for the
|
|
13
|
+
* environment's values; the proxy injects the key, and the app never holds it.
|
|
14
|
+
*/
|
|
15
|
+
export type ProxiedCredential = {
|
|
16
|
+
/** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
|
|
17
|
+
url: string;
|
|
18
|
+
/** `fetch`, at a path under the credential's upstream, with the handle the proxy takes. */
|
|
19
|
+
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
20
|
+
};
|
|
21
|
+
type Env = Record<string, string | undefined>;
|
|
22
|
+
/** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
|
|
23
|
+
export declare function credential(name: string, env?: Env, fetcher?: (input: string, init?: RequestInit) => Promise<Response>): ProxiedCredential;
|
|
24
|
+
//#endregion
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/credential.ts
|
|
2
|
+
/** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
|
|
3
|
+
function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input, init) => fetch(input, init)) {
|
|
4
|
+
let urls;
|
|
5
|
+
try {
|
|
6
|
+
urls = JSON.parse(env.COFFRE_CREDENTIALS ?? "{}");
|
|
7
|
+
} catch {
|
|
8
|
+
throw new Error("COFFRE_CREDENTIALS is not what `coffre run` sets: run the app with `coffre run <project>/<environment> -- …`");
|
|
9
|
+
}
|
|
10
|
+
const url = urls[name];
|
|
11
|
+
const handle = env.COFFRE_HANDLE;
|
|
12
|
+
if (typeof url !== "string" || handle === void 0) throw new Error(`no credential ${name} here: \`coffre run\` gives the app the credentials its environment holds, which the deployment's proxy names`);
|
|
13
|
+
return {
|
|
14
|
+
url,
|
|
15
|
+
fetch: (path, init = {}) => {
|
|
16
|
+
const headers = new Headers(init.headers);
|
|
17
|
+
headers.set("x-coffre-handle", handle);
|
|
18
|
+
return fetcher(`${url}${path.startsWith("/") ? "" : "/"}${path}`, {
|
|
19
|
+
...init,
|
|
20
|
+
headers
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { credential };
|