@pitininja/envious-typebox 6.0.0-beta10
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 +58 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +100 -0
- package/dist/index.js.map +1 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Envious Typebox resolver
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/@pitininja%2Fenvious-typebox)
|
|
4
|
+
|
|
5
|
+
> Only Typebox v1 or superior is supported
|
|
6
|
+
|
|
7
|
+
- [Install](#install)
|
|
8
|
+
- [Usage](#usage)
|
|
9
|
+
- [Formats](#formats)
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
npm i @pitininja/envious-typebox
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { envious } from '@pitininja/envious';
|
|
21
|
+
import { typeboxResolver } from '@pitininja/envious-typebox';
|
|
22
|
+
import { Type } from '@sinclair/typebox';
|
|
23
|
+
|
|
24
|
+
const env = envious({
|
|
25
|
+
resolver: typeboxResolver({
|
|
26
|
+
schema: Type.Object({
|
|
27
|
+
STRING_VAR: Type.String(),
|
|
28
|
+
NUMBER_VAR: Type.Integer(),
|
|
29
|
+
BOOLEAN_VAR_WITH_DEFAULT: Type.Boolean({ default: false }),
|
|
30
|
+
OPTIONAL_VAR: Type.Optional(Type.String())
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Formats
|
|
37
|
+
|
|
38
|
+
String formats can be loaded in the `formats` property of the Typebox resolver options, as an object with the format name as key and a regex or a validation function as value.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { envious } from '@pitininja/envious';
|
|
42
|
+
import { typeboxResolver } from '@pitininja/envious-typebox';
|
|
43
|
+
import { Type } from '@sinclair/typebox';
|
|
44
|
+
import dayjs from 'dayjs';
|
|
45
|
+
|
|
46
|
+
const env = envious({
|
|
47
|
+
resolver: typeboxResolver({
|
|
48
|
+
schema: Type.Object({
|
|
49
|
+
EXAMPLE_URI: Type.String({ format: 'uri' }),
|
|
50
|
+
EXAMPLE_DATE: Type.String({ format: 'date' })
|
|
51
|
+
}),
|
|
52
|
+
formats: {
|
|
53
|
+
uri: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i,
|
|
54
|
+
date: (val) => dayjs(val).isValid()
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
});
|
|
58
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Static, TObject } from 'typebox';
|
|
2
|
+
type EnviousTypeboxResolver<T extends TObject> = (env: NodeJS.ProcessEnv) => Static<T>;
|
|
3
|
+
export type EnviousTypeboxFormats = {
|
|
4
|
+
[name: string]: RegExp | ((val: unknown) => boolean);
|
|
5
|
+
};
|
|
6
|
+
export type EnviousTypeboxResolverOptions<T extends TObject> = {
|
|
7
|
+
schema: T;
|
|
8
|
+
formats?: EnviousTypeboxFormats;
|
|
9
|
+
};
|
|
10
|
+
export declare const typeboxResolver: <T extends TObject<import("typebox", { with: { "resolution-mode": "import" } }).TProperties>>({ schema, formats }: EnviousTypeboxResolverOptions<T>) => EnviousTypeboxResolver<T>;
|
|
11
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.typeboxResolver = void 0;
|
|
40
|
+
const envious_1 = require("@pitininja/envious");
|
|
41
|
+
const format_1 = __importDefault(require("typebox/format"));
|
|
42
|
+
const value_1 = __importStar(require("typebox/value"));
|
|
43
|
+
const setTypeboxFormats = (formats) => {
|
|
44
|
+
for (const [name, format] of Object.entries(formats)) {
|
|
45
|
+
format_1.default.Set(name, format instanceof RegExp ? (val) => format.test(val) : format);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
function checkIfObject(val) {
|
|
49
|
+
if (!val || typeof val !== 'object') {
|
|
50
|
+
throw new envious_1.EnviousError({
|
|
51
|
+
message: 'Could not parse environment variables'
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const transformTypeboxError = (typeboxError) => {
|
|
56
|
+
const { instancePath, message } = typeboxError;
|
|
57
|
+
return {
|
|
58
|
+
name: instancePath.replace(/^\//, ''),
|
|
59
|
+
message
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const typeboxResolver = ({ schema, formats }) => {
|
|
63
|
+
return (env) => {
|
|
64
|
+
try {
|
|
65
|
+
if (formats) {
|
|
66
|
+
setTypeboxFormats(formats);
|
|
67
|
+
}
|
|
68
|
+
const clonedEnv = value_1.default.Clone(env);
|
|
69
|
+
// Below we use object assertion and spread because Clean / Default / Convert functions return unknown referenced objects
|
|
70
|
+
const cleaned = value_1.default.Clean(schema, clonedEnv);
|
|
71
|
+
checkIfObject(cleaned);
|
|
72
|
+
const defaulted = value_1.default.Default(schema, { ...cleaned });
|
|
73
|
+
checkIfObject(defaulted);
|
|
74
|
+
const converted = value_1.default.Convert(schema, { ...defaulted });
|
|
75
|
+
checkIfObject(converted);
|
|
76
|
+
const errors = value_1.default.Errors(schema, { ...converted });
|
|
77
|
+
if (errors.length) {
|
|
78
|
+
throw new envious_1.EnviousError({
|
|
79
|
+
resolverErrors: errors,
|
|
80
|
+
transform: transformTypeboxError
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return value_1.default.Repair({
|
|
84
|
+
...schema,
|
|
85
|
+
additionalProperties: false
|
|
86
|
+
}, converted);
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
if (err instanceof value_1.AssertError) {
|
|
90
|
+
throw new envious_1.EnviousError({
|
|
91
|
+
resolverErrors: err.cause.errors,
|
|
92
|
+
transform: transformTypeboxError
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
exports.typeboxResolver = typeboxResolver;
|
|
100
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAkD;AAGlD,4DAAoC;AACpC,uDAAmD;AAenD,MAAM,iBAAiB,GAAG,CAAC,OAA8B,EAAE,EAAE,CAAC;IAC1D,KAAK,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,gBAAM,CAAC,GAAG,CACN,IAAI,EACJ,MAAM,YAAY,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAChE,CAAC;IACN,CAAC;AAAA,CACJ,CAAC;AAEF,SAAS,aAAa,CAAC,GAAY,EAAyB;IACxD,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAClC,MAAM,IAAI,sBAAY,CAAC;YACnB,OAAO,EAAE,uCAAuC;SACnD,CAAC,CAAC;IACP,CAAC;AAAA,CACJ;AAED,MAAM,qBAAqB,GAAG,CAAC,YAAuC,EAAE,EAAE,CAAC;IACvE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,GAAG,YAAY,CAAC;IAC/C,OAAO;QACH,IAAI,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;QACrC,OAAO;KACV,CAAC;AAAA,CACL,CAAC;AAEK,MAAM,eAAe,GAAG,CAAoB,EAC/C,MAAM,EACN,OAAO,EACwB,EAA6B,EAAE,CAAC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ACJ,CAAC;AAAA,CACL,CAAC;AAzCW,QAAA,eAAe,GAAf,eAAe,CAyC1B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pitininja/envious-typebox",
|
|
3
|
+
"version": "6.0.0-beta10",
|
|
4
|
+
"license": "AGPL-3.0-or-later",
|
|
5
|
+
"homepage": "https://codeberg.org/pitininja/envious",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://codeberg.org/pitininja/envious"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"./dist",
|
|
15
|
+
"./package.json"
|
|
16
|
+
],
|
|
17
|
+
"main": "./dist/resolvers/typebox/src/index.js",
|
|
18
|
+
"types": "./dist/resolvers/typebox/src/index.d.ts",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "npx tsgo --build --clean ./tsconfig.build.json && npx tsgo -b ./tsconfig.build.json"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@pitininja/envious": "^6.0.0-0",
|
|
24
|
+
"typebox": "^1.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|