Skip to content

Commit 84901ef

Browse files
committed
Introduced lifecycle hooks
Signed-off-by: Jean-Baptiste Bianchi <jb.bianchi@neuroglia.io>
1 parent 51a25ed commit 84901ef

File tree

194 files changed

+2619
-833
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+2619
-833
lines changed

src/lib/builder.ts

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,35 @@
1515
*
1616
*/
1717

18+
/**
19+
* Represents the options passed to the `build()` method
20+
*/
21+
export type BuildOptions = {
22+
/**
23+
* Default true
24+
*/
25+
validate?: boolean;
26+
/**
27+
* Default true
28+
*/
29+
normalize?: boolean;
30+
};
31+
32+
/**
33+
* The type of the underlying function called on `build()` for objects
34+
*/
35+
export type BuildingFunction<T> = (model: Partial<T>, options: BuildOptions) => T;
36+
37+
/**
38+
* The type of the underlying function called on `build()` for arrays
39+
*/
40+
export type ArrayBuildingFunction<T> = (model: Array<T>, options: BuildOptions) => Array<T>;
41+
1842
/**
1943
* Represents a fluent builder proxy for an object
2044
*/
2145
export type Builder<T> = {
22-
build: (validate?: boolean) => T;
46+
build: (option?: BuildOptions) => T;
2347
} & {
2448
[K in keyof T]-?: (arg: T[K]) => Builder<T>;
2549
};
@@ -29,15 +53,23 @@ export type Builder<T> = {
2953
*/
3054
export type ArrayBuilder<T> = {
3155
push: (item: T) => ArrayBuilder<T>;
32-
build: (validate?: boolean) => Array<T>;
56+
build: (option?: BuildOptions) => Array<T>;
3357
};
3458

3559
/**
3660
* The default function used to build an object, basically just return the provided object
3761
* @param model The object to "build"
62+
* @param options The build options
3863
* @returns
3964
*/
40-
function defaultBuildingFn<T>(model: Partial<T>): T {
65+
function defaultBuildingFn<T>(model: Partial<T>, options: BuildOptions): T {
66+
// prevents @typescript-eslint/no-unused-vars ...
67+
if (options.validate == null) {
68+
options.validate = true;
69+
}
70+
if (options.normalize == null) {
71+
options.normalize = true;
72+
}
4173
return model as T;
4274
}
4375

@@ -46,14 +78,20 @@ function defaultBuildingFn<T>(model: Partial<T>): T {
4678
* @param buildingFn The function used to validate and produce the object on build()
4779
* @returns A fluent builder
4880
*/
49-
export function builder<T>(
50-
model: Partial<T> = {},
51-
buildingFn: (data: Partial<T>) => T = defaultBuildingFn,
52-
): Builder<T> {
81+
export function builder<T>(model: Partial<T> = {}, buildingFn: BuildingFunction<T> = defaultBuildingFn): Builder<T> {
5382
const proxy = new Proxy({} as Builder<T>, {
5483
get: (_, prop) => {
5584
if (prop === 'build') {
56-
return (validate: boolean = true) => (validate ? buildingFn(model) : model);
85+
return (options?: BuildOptions) => {
86+
options = options || ({} as BuildOptions);
87+
if (options.validate == null) {
88+
options.validate = true;
89+
}
90+
if (options.normalize == null) {
91+
options.normalize = true;
92+
}
93+
return buildingFn(model, options);
94+
};
5795
}
5896
return (value: unknown): Builder<T> => {
5997
(model as any)[prop.toString()] = value;
@@ -74,15 +112,24 @@ export function builder<T>(
74112
*/
75113
export function arrayBuilder<T>(
76114
model: Array<T> = [],
77-
buildingFn: (data: Array<T>) => Array<T> = defaultBuildingFn,
115+
buildingFn: ArrayBuildingFunction<T> = defaultBuildingFn,
78116
): ArrayBuilder<T> {
79117
if (model != null && !Array.isArray(model)) {
80118
throw new Error(`The provided model should be an array`);
81119
}
82120
const proxy = new Proxy({} as ArrayBuilder<T>, {
83121
get: (_, prop) => {
84122
if (prop === 'build') {
85-
return (validate: boolean = true) => (validate ? buildingFn(model) : model);
123+
return (options?: BuildOptions) => {
124+
options = options || ({} as BuildOptions);
125+
if (options.validate == null) {
126+
options.validate = true;
127+
}
128+
if (options.normalize == null) {
129+
options.normalize = true;
130+
}
131+
return buildingFn(model, options);
132+
};
86133
}
87134
if (prop === 'push') {
88135
return (value: T): ArrayBuilder<T> => {

src/lib/generated/builders/authentication-policy-basic-builder.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.AuthenticationPolicyBasic} model The underlying object
31-
* @returns {Specification.AuthenticationPolicyBasic} The validated underlying object
29+
* @param {Specification.AuthenticationPolicyBasic} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.AuthenticationPolicyBasic} The built object
3232
*/
33-
function buildingFn(model: Specification.AuthenticationPolicyBasic): Specification.AuthenticationPolicyBasic {
33+
function buildingFn(
34+
model: Specification.AuthenticationPolicyBasic,
35+
options: BuildOptions,
36+
): Specification.AuthenticationPolicyBasic {
3437
const instance = new Classes.AuthenticationPolicyBasic(model);
35-
validate('AuthenticationPolicyBasic', instance);
36-
return instance as Specification.AuthenticationPolicyBasic;
38+
if (options.validate) instance.validate();
39+
return (options.normalize ? instance.normalize() : instance) as Specification.AuthenticationPolicyBasic;
3740
}
3841

3942
/**

src/lib/generated/builders/authentication-policy-bearer-builder.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.AuthenticationPolicyBearer} model The underlying object
31-
* @returns {Specification.AuthenticationPolicyBearer} The validated underlying object
29+
* @param {Specification.AuthenticationPolicyBearer} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.AuthenticationPolicyBearer} The built object
3232
*/
33-
function buildingFn(model: Specification.AuthenticationPolicyBearer): Specification.AuthenticationPolicyBearer {
33+
function buildingFn(
34+
model: Specification.AuthenticationPolicyBearer,
35+
options: BuildOptions,
36+
): Specification.AuthenticationPolicyBearer {
3437
const instance = new Classes.AuthenticationPolicyBearer(model);
35-
validate('AuthenticationPolicyBearer', instance);
36-
return instance as Specification.AuthenticationPolicyBearer;
38+
if (options.validate) instance.validate();
39+
return (options.normalize ? instance.normalize() : instance) as Specification.AuthenticationPolicyBearer;
3740
}
3841

3942
/**

src/lib/generated/builders/authentication-policy-builder.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.AuthenticationPolicy} model The underlying object
31-
* @returns {Specification.AuthenticationPolicy} The validated underlying object
29+
* @param {Specification.AuthenticationPolicy} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.AuthenticationPolicy} The built object
3232
*/
33-
function buildingFn(model: Specification.AuthenticationPolicy): Specification.AuthenticationPolicy {
33+
function buildingFn(
34+
model: Specification.AuthenticationPolicy,
35+
options: BuildOptions,
36+
): Specification.AuthenticationPolicy {
3437
const instance = new Classes.AuthenticationPolicy(model);
35-
validate('AuthenticationPolicy', instance);
36-
return instance as Specification.AuthenticationPolicy;
38+
if (options.validate) instance.validate();
39+
return (options.normalize ? instance.normalize() : instance) as Specification.AuthenticationPolicy;
3740
}
3841

3942
/**

src/lib/generated/builders/authentication-policy-oauth2-builder.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.AuthenticationPolicyOauth2} model The underlying object
31-
* @returns {Specification.AuthenticationPolicyOauth2} The validated underlying object
29+
* @param {Specification.AuthenticationPolicyOauth2} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.AuthenticationPolicyOauth2} The built object
3232
*/
33-
function buildingFn(model: Specification.AuthenticationPolicyOauth2): Specification.AuthenticationPolicyOauth2 {
33+
function buildingFn(
34+
model: Specification.AuthenticationPolicyOauth2,
35+
options: BuildOptions,
36+
): Specification.AuthenticationPolicyOauth2 {
3437
const instance = new Classes.AuthenticationPolicyOauth2(model);
35-
validate('AuthenticationPolicyOauth2', instance);
36-
return instance as Specification.AuthenticationPolicyOauth2;
38+
if (options.validate) instance.validate();
39+
return (options.normalize ? instance.normalize() : instance) as Specification.AuthenticationPolicyOauth2;
3740
}
3841

3942
/**

src/lib/generated/builders/authentication-policy-oauth2-client-builder.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.AuthenticationPolicyOauth2Client} model The underlying object
31-
* @returns {Specification.AuthenticationPolicyOauth2Client} The validated underlying object
29+
* @param {Specification.AuthenticationPolicyOauth2Client} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.AuthenticationPolicyOauth2Client} The built object
3232
*/
3333
function buildingFn(
3434
model: Specification.AuthenticationPolicyOauth2Client,
35+
options: BuildOptions,
3536
): Specification.AuthenticationPolicyOauth2Client {
3637
const instance = new Classes.AuthenticationPolicyOauth2Client(model);
37-
validate('AuthenticationPolicyOauth2Client', instance);
38-
return instance as Specification.AuthenticationPolicyOauth2Client;
38+
if (options.validate) instance.validate();
39+
return (options.normalize ? instance.normalize() : instance) as Specification.AuthenticationPolicyOauth2Client;
3940
}
4041

4142
/**

src/lib/generated/builders/call-async-api-builder.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.CallAsyncAPI} model The underlying object
31-
* @returns {Specification.CallAsyncAPI} The validated underlying object
29+
* @param {Specification.CallAsyncAPI} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.CallAsyncAPI} The built object
3232
*/
33-
function buildingFn(model: Specification.CallAsyncAPI): Specification.CallAsyncAPI {
33+
function buildingFn(model: Specification.CallAsyncAPI, options: BuildOptions): Specification.CallAsyncAPI {
3434
const instance = new Classes.CallAsyncAPI(model);
35-
validate('CallAsyncAPI', instance);
36-
return instance as Specification.CallAsyncAPI;
35+
if (options.validate) instance.validate();
36+
return (options.normalize ? instance.normalize() : instance) as Specification.CallAsyncAPI;
3737
}
3838

3939
/**

src/lib/generated/builders/call-async-api-with-authentication-builder.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.CallAsyncAPIWithAuthentication} model The underlying object
31-
* @returns {Specification.CallAsyncAPIWithAuthentication} The validated underlying object
29+
* @param {Specification.CallAsyncAPIWithAuthentication} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.CallAsyncAPIWithAuthentication} The built object
3232
*/
33-
function buildingFn(model: Specification.CallAsyncAPIWithAuthentication): Specification.CallAsyncAPIWithAuthentication {
33+
function buildingFn(
34+
model: Specification.CallAsyncAPIWithAuthentication,
35+
options: BuildOptions,
36+
): Specification.CallAsyncAPIWithAuthentication {
3437
const instance = new Classes.CallAsyncAPIWithAuthentication(model);
35-
validate('CallAsyncAPIWithAuthentication', instance);
36-
return instance as Specification.CallAsyncAPIWithAuthentication;
38+
if (options.validate) instance.validate();
39+
return (options.normalize ? instance.normalize() : instance) as Specification.CallAsyncAPIWithAuthentication;
3740
}
3841

3942
/**

src/lib/generated/builders/call-async-api-with-builder.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.CallAsyncAPIWith} model The underlying object
31-
* @returns {Specification.CallAsyncAPIWith} The validated underlying object
29+
* @param {Specification.CallAsyncAPIWith} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.CallAsyncAPIWith} The built object
3232
*/
33-
function buildingFn(model: Specification.CallAsyncAPIWith): Specification.CallAsyncAPIWith {
33+
function buildingFn(model: Specification.CallAsyncAPIWith, options: BuildOptions): Specification.CallAsyncAPIWith {
3434
const instance = new Classes.CallAsyncAPIWith(model);
35-
validate('CallAsyncAPIWith', instance);
36-
return instance as Specification.CallAsyncAPIWith;
35+
if (options.validate) instance.validate();
36+
return (options.normalize ? instance.normalize() : instance) as Specification.CallAsyncAPIWith;
3737
}
3838

3939
/**

src/lib/generated/builders/call-function-builder.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
*
2121
*****************************************************************************************/
2222

23-
import { builder, Builder } from '../../builder';
24-
import { validate } from '../../validation';
23+
import { builder, Builder, BuildOptions } from '../../builder';
2524
import { Classes } from '../classes';
2625
import { Specification } from '../definitions';
2726

2827
/**
2928
* The internal function used by the builder proxy to validate and return its underlying object
30-
* @param {Specification.CallFunction} model The underlying object
31-
* @returns {Specification.CallFunction} The validated underlying object
29+
* @param {Specification.CallFunction} model The proxied object
30+
* @param {BuildOptions} options The build options to use
31+
* @returns {Specification.CallFunction} The built object
3232
*/
33-
function buildingFn(model: Specification.CallFunction): Specification.CallFunction {
33+
function buildingFn(model: Specification.CallFunction, options: BuildOptions): Specification.CallFunction {
3434
const instance = new Classes.CallFunction(model);
35-
validate('CallFunction', instance);
36-
return instance as Specification.CallFunction;
35+
if (options.validate) instance.validate();
36+
return (options.normalize ? instance.normalize() : instance) as Specification.CallFunction;
3737
}
3838

3939
/**

0 commit comments

Comments
 (0)