@clerc/advanced-types 0.0.1 → 1.0.0-beta.29
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/LICENSE +21 -0
- package/README.md +15 -34
- package/dist/index.d.ts +55 -0
- package/dist/index.js +63 -0
- package/package.json +42 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Ray <https://github.com/so1ve>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,45 +1,26 @@
|
|
|
1
1
|
# @clerc/advanced-types
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@clerc/advanced-types)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Advanced runtime types for Clerc.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Exports additional types that can be used to validate and parse data at runtime, extending the capabilities of the core Clerc library.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
> [!NOTE]
|
|
10
|
+
> You don't have to install this package separately if you are already using `@clerc/core` package as it has re-exported all the types from this package.
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
1. Configure OIDC trusted publishing for the package name `@clerc/advanced-types`
|
|
13
|
-
2. Enable secure, token-less publishing from CI/CD workflows
|
|
14
|
-
3. Establish provenance for packages published under this name
|
|
12
|
+
## 📦 Installation
|
|
15
13
|
|
|
16
|
-
|
|
14
|
+
```bash
|
|
15
|
+
$ npm install @clerc/advanced-types
|
|
16
|
+
$ yarn add @clerc/advanced-types
|
|
17
|
+
$ pnpm add @clerc/advanced-types
|
|
18
|
+
```
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
## 🚀 Usage
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
Please refer to [src/index.ts](src/index.ts).
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
## 📝 License
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
2. Configure the trusted publisher (e.g., GitHub Actions)
|
|
26
|
-
3. Specify the repository and workflow that should be allowed to publish
|
|
27
|
-
4. Use the configured workflow to publish your actual package
|
|
28
|
-
|
|
29
|
-
## DO NOT USE THIS PACKAGE
|
|
30
|
-
|
|
31
|
-
This package is a placeholder for OIDC configuration only. It:
|
|
32
|
-
- Contains no executable code
|
|
33
|
-
- Provides no functionality
|
|
34
|
-
- Should not be installed as a dependency
|
|
35
|
-
- Exists only for administrative purposes
|
|
36
|
-
|
|
37
|
-
## More Information
|
|
38
|
-
|
|
39
|
-
For more details about npm's trusted publishing feature, see:
|
|
40
|
-
- [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
|
|
41
|
-
- [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
|
|
42
|
-
|
|
43
|
-
---
|
|
44
|
-
|
|
45
|
-
**Maintained for OIDC setup purposes only**
|
|
26
|
+
[MIT](../../LICENSE). Made with ❤️ by [Ray](https://github.com/so1ve)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region ../parser/src/types.d.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Defines how a string input is converted to the target type T.
|
|
5
|
+
*
|
|
6
|
+
* @template T The target type.
|
|
7
|
+
*/
|
|
8
|
+
interface TypeFunction<T = unknown> {
|
|
9
|
+
(value: string): T;
|
|
10
|
+
/**
|
|
11
|
+
* Optional display name for the type, useful in help output.
|
|
12
|
+
* If provided, this will be shown instead of the function name.
|
|
13
|
+
*/
|
|
14
|
+
display?: string;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/errors.d.ts
|
|
18
|
+
declare class FlagValidationError extends Error {}
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/index.d.ts
|
|
21
|
+
/**
|
|
22
|
+
* Creates a Enum type function that validates the input against allowed values.
|
|
23
|
+
* The display name will be formatted as "value1 | value2 | ..." for help output.
|
|
24
|
+
*
|
|
25
|
+
* @param values - Array of allowed string values
|
|
26
|
+
* @returns A TypeFunction that validates and returns the input value
|
|
27
|
+
* @throws {Error} If the value is not in the allowed values list
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const format = Enum(['json', 'yaml', 'xml']);
|
|
32
|
+
* // Help output will show: json | yaml | xml
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare function Enum<T extends string>(...values: T[]): TypeFunction<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a range type function that validates the input is a number within the specified range.
|
|
38
|
+
*
|
|
39
|
+
* @param min - The minimum acceptable value (inclusive)
|
|
40
|
+
* @param max - The maximum acceptable value (inclusive)
|
|
41
|
+
* @returns A TypeFunction that validates the input value
|
|
42
|
+
* @throws {Error} If the value is not a number or is outside the specified range
|
|
43
|
+
*/
|
|
44
|
+
declare function Range(min: number, max: number): TypeFunction<number>;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a regex type function that validates the input against the provided pattern.
|
|
47
|
+
*
|
|
48
|
+
* @param pattern - The regular expression pattern to validate against
|
|
49
|
+
* @param description - Optional description for display purposes
|
|
50
|
+
* @returns A TypeFunction that validates the input value
|
|
51
|
+
* @throws {Error} If the value does not match the regex pattern
|
|
52
|
+
*/
|
|
53
|
+
declare function Regex(pattern: RegExp, description?: string): TypeFunction<string>;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { Enum, FlagValidationError, Range, Regex };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//#region src/errors.ts
|
|
2
|
+
var FlagValidationError = class extends Error {};
|
|
3
|
+
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
/**
|
|
7
|
+
* Creates a Enum type function that validates the input against allowed values.
|
|
8
|
+
* The display name will be formatted as "value1 | value2 | ..." for help output.
|
|
9
|
+
*
|
|
10
|
+
* @param values - Array of allowed string values
|
|
11
|
+
* @returns A TypeFunction that validates and returns the input value
|
|
12
|
+
* @throws {Error} If the value is not in the allowed values list
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const format = Enum(['json', 'yaml', 'xml']);
|
|
17
|
+
* // Help output will show: json | yaml | xml
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
function Enum(...values) {
|
|
21
|
+
const fn = ((value) => {
|
|
22
|
+
if (!values.includes(value)) throw new FlagValidationError(`Invalid value: ${value}. Must be one of: ${values.join(", ")}`);
|
|
23
|
+
return value;
|
|
24
|
+
});
|
|
25
|
+
fn.display = values.join(" | ");
|
|
26
|
+
return fn;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a range type function that validates the input is a number within the specified range.
|
|
30
|
+
*
|
|
31
|
+
* @param min - The minimum acceptable value (inclusive)
|
|
32
|
+
* @param max - The maximum acceptable value (inclusive)
|
|
33
|
+
* @returns A TypeFunction that validates the input value
|
|
34
|
+
* @throws {Error} If the value is not a number or is outside the specified range
|
|
35
|
+
*/
|
|
36
|
+
function Range(min, max) {
|
|
37
|
+
const fn = ((value) => {
|
|
38
|
+
const num = Number(value);
|
|
39
|
+
if (Number.isNaN(num) || num < min || num > max) throw new FlagValidationError(`Invalid value: ${value}. Must be a number between ${min} and ${max}`);
|
|
40
|
+
return num;
|
|
41
|
+
});
|
|
42
|
+
fn.display = `${min} - ${max}`;
|
|
43
|
+
return fn;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Creates a regex type function that validates the input against the provided pattern.
|
|
47
|
+
*
|
|
48
|
+
* @param pattern - The regular expression pattern to validate against
|
|
49
|
+
* @param description - Optional description for display purposes
|
|
50
|
+
* @returns A TypeFunction that validates the input value
|
|
51
|
+
* @throws {Error} If the value does not match the regex pattern
|
|
52
|
+
*/
|
|
53
|
+
function Regex(pattern, description) {
|
|
54
|
+
const fn = ((value) => {
|
|
55
|
+
if (!pattern.test(value)) throw new FlagValidationError(`Invalid value: ${value}. Must match pattern: ${pattern}`);
|
|
56
|
+
return value;
|
|
57
|
+
});
|
|
58
|
+
fn.display = description ?? `Regex: ${pattern.toString()}`;
|
|
59
|
+
return fn;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
export { Enum, FlagValidationError, Range, Regex };
|
package/package.json
CHANGED
|
@@ -1,10 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/advanced-types",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
3
|
+
"version": "1.0.0-beta.29",
|
|
4
|
+
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Clerc advanced types",
|
|
5
7
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
"args",
|
|
9
|
+
"arguments",
|
|
10
|
+
"argv",
|
|
11
|
+
"clerc",
|
|
12
|
+
"cli"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/clercjs/clerc/tree/main/packages/advanced-types#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/clercjs/clerc.git",
|
|
18
|
+
"directory": "/"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/clercjs/clerc/issues"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"module": "./dist/index.js",
|
|
30
|
+
"types": "dist/index.d.ts",
|
|
31
|
+
"typesVersions": {
|
|
32
|
+
"*": {
|
|
33
|
+
"*": [
|
|
34
|
+
"./dist/*",
|
|
35
|
+
"./dist/index.d.ts"
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
}
|
|
45
|
+
}
|