Skip to content

Commit 07b5a1a

Browse files
committed
refactor: change API
BREAKING CHANGE: remove createTypedParameters, export TypedParameters class
1 parent 3c4f22a commit 07b5a1a

File tree

6 files changed

+3014
-3744
lines changed

6 files changed

+3014
-3744
lines changed

README.md

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Commitizen Friendly][commitizen-img]][commitizen-url]
99
[![Semantic Release][semantic-release-img]][semantic-release-url]
1010

11-
> Type-safe parameter construction library. Useful for managing environment variables, aws parameter stores and more.
11+
> Type-safe parameters construction library. Useful for managing Query Parameters, Environment Variables, AWS System Manager Parameter Store, and more.
1212
1313
## Install
1414

@@ -18,10 +18,12 @@ npm install construct-typed-parameters
1818

1919
## Usage
2020

21+
### Basic
22+
2123
```ts
22-
import { createTypedParameters } from 'construct-typed-parameters';
24+
import { TypedParameters } from 'construct-typed-parameters';
2325

24-
const parameters = createTypedParameters(parameterType => ({
26+
const parameters = new TypedParameters(parameterType => ({
2527
TOKEN: parameterType.String({ required: true }),
2628
FIREBASE_CONFIG: parameterType.Json<{ apiKey: string }>({ required: true }),
2729
}));
@@ -32,41 +34,76 @@ const stringifiedParameters = parameters.stringify({
3234
});
3335
//=> { TOKEN: 'xxxx', FIREBASE_CONFIG: '{"apiKey":"xxxx"}'}
3436

35-
// set to Environment Variable
36-
Object.entries(stringifiedParameters).forEach(
37-
([parameterName, stringifiedValue]) => {
38-
process.env[parameterName] = stringifiedValue;
39-
}
37+
const parsedParameters = parameters.parse({
38+
TOKEN: 'xxxx',
39+
FIREBASE_CONFIG: '{"apiKey":"xxxx"}',
40+
});
41+
//=> { TOKEN: 'xxxx', FIREBASE_CONFIG: { apiKey: 'xxxx' }}
42+
```
43+
44+
#### AutoCompletion
45+
46+
![AutoCompletion](https://github.com/masahirompp/images/blob/main/construct-typed-parameters.png?raw=true)
47+
48+
### with Query Parameters
49+
50+
```ts
51+
const queryString = new URLSearchParams(
52+
parameters.stringify({
53+
TOKEN: 'xxxx',
54+
FIREBASE_CONFIG: { apiKey: 'xxxx' },
55+
})
56+
).toString();
57+
//=> 'TOKEN=xxxx&FIREBASE_CONFIG=%7B%22apiKey%22%3A%22xxxx%22%7D'
58+
59+
const parsedParameters = parameters.parse(
60+
Object.fromEntries(
61+
new URLSearchParams(
62+
'TOKEN=xxxx&FIREBASE_CONFIG=%7B%22apiKey%22%3A%22xxxx%22%7D'
63+
).entries()
64+
)
4065
);
66+
//=> { TOKEN: 'xxxx', FIREBASE_CONFIG: { apiKey: 'xxxx' } }
67+
```
68+
69+
### with Environment Variables
70+
71+
```ts
72+
Object.entries(
73+
parameters.stringify({
74+
TOKEN: 'xxxx',
75+
FIREBASE_CONFIG: { apiKey: 'xxxx' },
76+
})
77+
).forEach(([parameterName, stringifiedValue]) => {
78+
process.env[parameterName] = stringifiedValue;
79+
});
4180
//=>
4281
// process.env.TOKEN: 'xxxx'
4382
// process.env.FIREBASE_CONFIG: '{"apiKey":"xxxx"}'
4483

45-
// load from Environment Variable
4684
const parsedParameters = parameters.parse({
4785
TOKEN: process.env.TOKEN,
4886
FIREBASE_CONFIG: process.env.FIREBASE_CONFIG,
4987
});
5088
//=> { TOKEN: 'xxxx', FIREBASE_CONFIG: { apiKey: 'xxxx' } }
5189
```
5290

53-
## AutoCompletion
91+
### with AWS SSM Parameter Store
5492

55-
![AutoCompletion](https://github.com/masahirompp/construct-typed-parameters/blob/image/autocompletion.png?raw=true)
93+
see <https://github.com/masahirompp/ssm-parameters-boot>
5694

5795
## API
5896

5997
see `test/index.spec.ts`.
6098

61-
### createTypedParameters
99+
### TypedParameters
100+
101+
#### Constructor
62102

63103
```ts
64-
import {
65-
createTypedParameters,
66-
TypedParametersConstruct,
67-
} from 'construct-typed-parameters';
104+
import { TypedParameters } from 'construct-typed-parameters';
68105

69-
const parameters: TypedParametersConstruct<T> = createTypedParameters(pt => ({
106+
const parameters = new TypedParameters(pt => ({
70107
stringValue: pt.String({
71108
// required: boolean
72109
required: true,
@@ -79,12 +116,12 @@ const parameters: TypedParametersConstruct<T> = createTypedParameters(pt => ({
79116
required: true,
80117
defaultValue: 'v1',
81118
validate: v =>
82-
['v1', 'v2'].includes(v) ? null : ['the value must be v1 or v2'],
119+
['v1', 'v2'].includes(v) ? null : 'the value must be v1 or v2',
83120
}),
84121
numberValue: pt.Number({
85122
required: true,
86123
defaultValue: 1,
87-
validate: v => (v === 0 ? 'value must not be 0' : ''),
124+
validate: v => (v === 0 ? 'value must not be 0' : null),
88125
}),
89126
unionNumberValue: pt.UnionNumber<0 | 1>({
90127
required: true,
@@ -99,31 +136,23 @@ const parameters: TypedParametersConstruct<T> = createTypedParameters(pt => ({
99136
jsonValue: pt.Json<{ apiKey: string }>({
100137
required: true,
101138
defaultValue: { apiKey: 'xxxx' },
102-
validate: v => (v.apiKey.length ? '' : 'apiKey must be specified'),
139+
validate: v => (v.apiKey.length ? null : 'apiKey must be specified'),
103140
}),
104141
arrayValue: pt.Json<string[]>({
105142
required: true,
106143
defaultValue: ['main', 'sub'],
107-
validate: v => (v.length ? [] : ['array must not empty']),
144+
validate: v => (v.length ? null : 'array must not empty'),
108145
}),
109146
}));
110147
```
111148

112-
### TypedParametersConstruct#parse
149+
#### Method
113150

114151
```ts
115-
const parameters: TypedParametersConstruct<T> = createTypedParameters(pt => ({ ... }));
116-
117152
parameters.parse(
118153
stringifiedParameters: Partial<StringifiedParameters<T>>,
119154
shouldValidate = true
120155
) : ParsedParameters<T>
121-
```
122-
123-
### TypedParametersConstruct#stringify
124-
125-
```ts
126-
const parameters: TypedParametersConstruct<T> = createTypedParameters(pt => ({ ... }));
127156

128157
parameters.stringify(
129158
parsedParameters: Partial<ParsedParameters<T>>,

0 commit comments

Comments
 (0)