@awsmbiz/inbis-cloudflare 0.0.0-stage → 0.1.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/README.md +19 -2
- package/dist/index.d.ts +19 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -3
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @awsmbiz/inbis-cloudflare
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Typed helpers for reading a Worker's `env`. Packages never read bindings by global name;
|
|
4
|
+
the app passes the `env` it was given per request.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { requireBinding, requireSecret, requireVar } from "@awsmbiz/inbis-cloudflare";
|
|
8
|
+
|
|
9
|
+
const db = requireBinding(env, "DB"); // D1Database (non-null)
|
|
10
|
+
const secret = requireSecret(env, "AUTH_SECRET"); // set with `wrangler secret put`
|
|
11
|
+
const origins = requireVar(env, "TRUSTED_ORIGINS"); // `vars` in wrangler.jsonc
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Names are checked against the `Env` type (generate it with `wrangler types`).
|
|
15
|
+
`requireSecret` and `requireVar` only accept keys whose type is a string.
|
|
16
|
+
|
|
17
|
+
When a binding is missing, or a secret or variable is missing or blank, the helpers throw
|
|
18
|
+
an `AppError` with `500 CONFIGURATION_ERROR` and the message "The server is
|
|
19
|
+
misconfigured.". The binding name appears only in the error's `cause`, which is logged
|
|
20
|
+
but never returned, and secret values are never included.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type StringKeys<E> = { [K in keyof E]-?: NonNullable<E[K]> extends string ? K : never; }[keyof E] & string;
|
|
2
|
+
/**
|
|
3
|
+
* Returns `env[name]`, or throws a 500 `CONFIGURATION_ERROR` when the binding
|
|
4
|
+
* is missing from the Worker's configuration.
|
|
5
|
+
*/
|
|
6
|
+
export declare function requireBinding<E extends object, K extends keyof E & string>(env: E, name: K): NonNullable<E[K]>;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the Worker secret `name` (set with `wrangler secret put`), or throws
|
|
9
|
+
* a 500 `CONFIGURATION_ERROR` when it is missing or blank. The value is never
|
|
10
|
+
* included in the error.
|
|
11
|
+
*/
|
|
12
|
+
export declare function requireSecret<E extends object>(env: E, name: StringKeys<E>): string;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the plain-text Worker variable `name` (`vars` in the wrangler
|
|
15
|
+
* config), or throws a 500 `CONFIGURATION_ERROR` when it is missing or blank.
|
|
16
|
+
*/
|
|
17
|
+
export declare function requireVar<E extends object>(env: E, name: StringKeys<E>): string;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AppError } from "@awsmbiz/inbis-core";
|
|
2
|
+
// src/env.ts
|
|
3
|
+
function misconfigured(detail) {
|
|
4
|
+
return new AppError({
|
|
5
|
+
code: "CONFIGURATION_ERROR",
|
|
6
|
+
status: 500,
|
|
7
|
+
message: "The server is misconfigured.",
|
|
8
|
+
cause: new Error(detail)
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Returns `env[name]`, or throws a 500 `CONFIGURATION_ERROR` when the binding
|
|
13
|
+
* is missing from the Worker's configuration.
|
|
14
|
+
*/
|
|
15
|
+
function requireBinding(env, name) {
|
|
16
|
+
const value = env[name];
|
|
17
|
+
if (value === void 0 || value === null) throw misconfigured(`Missing binding: ${name}`);
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
function requireString(env, name, kind) {
|
|
21
|
+
const value = env[name];
|
|
22
|
+
if (typeof value !== "string" || value.trim() === "") throw misconfigured(`Missing or blank ${kind}: ${name}`);
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns the Worker secret `name` (set with `wrangler secret put`), or throws
|
|
27
|
+
* a 500 `CONFIGURATION_ERROR` when it is missing or blank. The value is never
|
|
28
|
+
* included in the error.
|
|
29
|
+
*/
|
|
30
|
+
function requireSecret(env, name) {
|
|
31
|
+
return requireString(env, name, "secret");
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Returns the plain-text Worker variable `name` (`vars` in the wrangler
|
|
35
|
+
* config), or throws a 500 `CONFIGURATION_ERROR` when it is missing or blank.
|
|
36
|
+
*/
|
|
37
|
+
function requireVar(env, name) {
|
|
38
|
+
return requireString(env, name, "variable");
|
|
39
|
+
}
|
|
40
|
+
export { requireBinding, requireSecret, requireVar };
|
|
41
|
+
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/env.ts"],"sourcesContent":["import { AppError } from \"@awsmbiz/inbis-core\";\n\ntype StringKeys<E> = { [K in keyof E]-?: NonNullable<E[K]> extends string ? K : never }[keyof E] &\n string;\n\nfunction misconfigured(detail: string): AppError {\n // The caller-facing message never names the binding; the cause is for logs only.\n return new AppError({\n code: \"CONFIGURATION_ERROR\",\n status: 500,\n message: \"The server is misconfigured.\",\n cause: new Error(detail),\n });\n}\n\n/**\n * Returns `env[name]`, or throws a 500 `CONFIGURATION_ERROR` when the binding\n * is missing from the Worker's configuration.\n */\nexport function requireBinding<E extends object, K extends keyof E & string>(\n env: E,\n name: K,\n): NonNullable<E[K]> {\n const value = env[name];\n if (value === undefined || value === null) {\n throw misconfigured(`Missing binding: ${name}`);\n }\n return value;\n}\n\nfunction requireString<E extends object>(env: E, name: StringKeys<E>, kind: string): string {\n const value: unknown = env[name];\n if (typeof value !== \"string\" || value.trim() === \"\") {\n throw misconfigured(`Missing or blank ${kind}: ${name}`);\n }\n return value;\n}\n\n/**\n * Returns the Worker secret `name` (set with `wrangler secret put`), or throws\n * a 500 `CONFIGURATION_ERROR` when it is missing or blank. The value is never\n * included in the error.\n */\nexport function requireSecret<E extends object>(env: E, name: StringKeys<E>): string {\n return requireString(env, name, \"secret\");\n}\n\n/**\n * Returns the plain-text Worker variable `name` (`vars` in the wrangler\n * config), or throws a 500 `CONFIGURATION_ERROR` when it is missing or blank.\n */\nexport function requireVar<E extends object>(env: E, name: StringKeys<E>): string {\n return requireString(env, name, \"variable\");\n}\n"],"mappings":";;AAKA,SAAS,cAAc,QAA0B;CAE/C,OAAO,IAAI,SAAS;EAClB,MAAM;EACN,QAAQ;EACR,SAAS;EACT,OAAO,IAAI,MAAM,MAAM;CACzB,CAAC;AACH;;;;;AAMA,SAAgB,eACd,KACA,MACmB;CACnB,MAAM,QAAQ,IAAI;CAClB,IAAI,UAAU,KAAA,KAAa,UAAU,MACnC,MAAM,cAAc,oBAAoB,MAAM;CAEhD,OAAO;AACT;AAEA,SAAS,cAAgC,KAAQ,MAAqB,MAAsB;CAC1F,MAAM,QAAiB,IAAI;CAC3B,IAAI,OAAO,UAAU,YAAY,MAAM,KAAK,MAAM,IAChD,MAAM,cAAc,oBAAoB,KAAK,IAAI,MAAM;CAEzD,OAAO;AACT;;;;;;AAOA,SAAgB,cAAgC,KAAQ,MAA6B;CACnF,OAAO,cAAc,KAAK,MAAM,QAAQ;AAC1C;;;;;AAMA,SAAgB,WAA6B,KAAQ,MAA6B;CAChF,OAAO,cAAc,KAAK,MAAM,UAAU;AAC5C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@awsmbiz/inbis-cloudflare",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Cloudflare Workers helpers for Inbis packages: typed access to bindings, variables and secrets.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/awsmbiz/inbis-platform.git",
|
|
9
|
+
"directory": "packages/cloudflare"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"registry": "https://registry.npmjs.org/",
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@awsmbiz/inbis-core": "0.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
32
|
+
"@cloudflare/vitest-plugin": "1.4.11",
|
|
33
|
+
"@cloudflare/workers-types": "5.20261030.1",
|
|
34
|
+
"@awsmbiz/inbis-eslint-config": "0.1.0",
|
|
35
|
+
"@awsmbiz/inbis-tsconfig": "0.1.0",
|
|
36
|
+
"eslint": "10.14.0",
|
|
37
|
+
"publint": "0.3.25",
|
|
38
|
+
"tsdown": "0.23.4",
|
|
39
|
+
"typescript": "6.0.3",
|
|
40
|
+
"vitest": "5.0.5"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsdown",
|
|
44
|
+
"typecheck": "tsc",
|
|
45
|
+
"lint": "eslint . --max-warnings 0",
|
|
46
|
+
"test": "vitest run",
|
|
47
|
+
"check:exports": "publint --strict && attw --pack . --profile esm-only"
|
|
48
|
+
}
|
|
6
49
|
}
|