Skip to content

Commit d7704ba

Browse files
committed
Extended support to array types
Signed-off-by: Jean-Baptiste Bianchi <jb.bianchi@neuroglia.io>
1 parent 0e5d469 commit d7704ba

File tree

199 files changed

+3588
-1387
lines changed

Some content is hidden

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

199 files changed

+3588
-1387
lines changed

src/lib/builder.ts

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,47 @@
1616
*/
1717

1818
/**
19-
* Represents a fluent builder proxy
19+
* Represents a fluent builder proxy for an object
2020
*/
2121
export type Builder<T> = {
2222
build: (validate?: boolean) => T;
2323
} & {
2424
[K in keyof T]-?: (arg: T[K]) => Builder<T>;
2525
};
2626

27+
/**
28+
* Represents a fluent builder proxy for an array
29+
*/
30+
export type ArrayBuilder<T> = {
31+
push: (item: T) => ArrayBuilder<T>;
32+
build: (validate?: boolean) => Array<T>;
33+
};
34+
2735
/**
2836
* The default function used to build an object, basically just return the provided object
29-
* @param data The object to "build"
37+
* @param model The object to "build"
3038
* @returns
3139
*/
32-
function defaultBuildingFn<T>(data: Partial<T>): T {
33-
return data as T;
40+
function defaultBuildingFn<T>(model: Partial<T>): T {
41+
return model as T;
3442
}
3543

3644
/**
3745
* A factory for fluent builders that proxy properties assignations and can validate against schema on build()
38-
* @param {Function} buildingFn The function used to validate and produce the object on build()
39-
* @returns {Builder} A fluent builder
46+
* @param buildingFn The function used to validate and produce the object on build()
47+
* @returns A fluent builder
4048
*/
41-
export function builder<T>(buildingFn?: (data: Partial<T>) => T): Builder<T> {
42-
const data: Partial<T> = {};
49+
export function builder<T>(
50+
model: Partial<T> = {},
51+
buildingFn: (data: Partial<T>) => T = defaultBuildingFn,
52+
): Builder<T> {
4353
const proxy = new Proxy({} as Builder<T>, {
4454
get: (_, prop) => {
4555
if (prop === 'build') {
46-
return (validate: boolean = true) => (validate ? (buildingFn || defaultBuildingFn)(data) : data);
56+
return (validate: boolean = true) => (validate ? buildingFn(model) : model);
4757
}
4858
return (value: unknown): Builder<T> => {
49-
(data as any)[prop.toString()] = value;
59+
(model as any)[prop.toString()] = value;
5060
return proxy;
5161
};
5262
},
@@ -56,3 +66,34 @@ export function builder<T>(buildingFn?: (data: Partial<T>) => T): Builder<T> {
5666
});
5767
return proxy;
5868
}
69+
70+
/**
71+
* A factory for fluent builders that proxy properties assignations and can validate against schema on build()
72+
* @param buildingFn The function used to validate and produce the object on build()
73+
* @returns A fluent builder
74+
*/
75+
export function arrayBuilder<T>(
76+
model: Array<T> = [],
77+
buildingFn: (data: Array<T>) => Array<T> = defaultBuildingFn,
78+
): ArrayBuilder<T> {
79+
if (model != null && !Array.isArray(model)) {
80+
throw new Error(`The provided model should be an array`);
81+
}
82+
const proxy = new Proxy({} as ArrayBuilder<T>, {
83+
get: (_, prop) => {
84+
if (prop === 'build') {
85+
return (validate: boolean = true) => (validate ? buildingFn(model) : model);
86+
}
87+
if (prop === 'push') {
88+
return (value: T): ArrayBuilder<T> => {
89+
model.push(value);
90+
return proxy;
91+
};
92+
}
93+
},
94+
set: () => {
95+
return false;
96+
},
97+
});
98+
return proxy;
99+
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.AuthenticationPolicyBasic} data The underlying object
24+
* @param {Specification.AuthenticationPolicyBasic} model The underlying object
2525
* @returns {Specification.AuthenticationPolicyBasic} The validated underlying object
2626
*/
27-
function buildingFn(data: Specification.AuthenticationPolicyBasic): Specification.AuthenticationPolicyBasic {
28-
const model = new Classes.AuthenticationPolicyBasic(data);
29-
30-
validate('AuthenticationPolicyBasic', model);
31-
return model as Specification.AuthenticationPolicyBasic;
27+
function buildingFn(model: Specification.AuthenticationPolicyBasic): Specification.AuthenticationPolicyBasic {
28+
const instance = new Classes.AuthenticationPolicyBasic(model);
29+
validate('AuthenticationPolicyBasic', instance);
30+
return instance as Specification.AuthenticationPolicyBasic;
3231
}
3332

3433
/**
3534
* A factory to create a builder proxy for the type `Specification.AuthenticationPolicyBasic`
36-
* @returns {Specification.AuthenticationPolicyBasic} A builder for `Specification.AuthenticationPolicyBasic`
35+
* @returns {Builder<Specification.AuthenticationPolicyBasic>} A builder for `Specification.AuthenticationPolicyBasic`
3736
*/
38-
export const authenticationPolicyBasicBuilder = (): Builder<Specification.AuthenticationPolicyBasic> =>
39-
builder<Specification.AuthenticationPolicyBasic>(buildingFn);
37+
export const authenticationPolicyBasicBuilder = (
38+
model?: Partial<Specification.AuthenticationPolicyBasic>,
39+
): Builder<Specification.AuthenticationPolicyBasic> =>
40+
builder<Specification.AuthenticationPolicyBasic>(model, buildingFn);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.AuthenticationPolicyBearer} data The underlying object
24+
* @param {Specification.AuthenticationPolicyBearer} model The underlying object
2525
* @returns {Specification.AuthenticationPolicyBearer} The validated underlying object
2626
*/
27-
function buildingFn(data: Specification.AuthenticationPolicyBearer): Specification.AuthenticationPolicyBearer {
28-
const model = new Classes.AuthenticationPolicyBearer(data);
29-
30-
validate('AuthenticationPolicyBearer', model);
31-
return model as Specification.AuthenticationPolicyBearer;
27+
function buildingFn(model: Specification.AuthenticationPolicyBearer): Specification.AuthenticationPolicyBearer {
28+
const instance = new Classes.AuthenticationPolicyBearer(model);
29+
validate('AuthenticationPolicyBearer', instance);
30+
return instance as Specification.AuthenticationPolicyBearer;
3231
}
3332

3433
/**
3534
* A factory to create a builder proxy for the type `Specification.AuthenticationPolicyBearer`
36-
* @returns {Specification.AuthenticationPolicyBearer} A builder for `Specification.AuthenticationPolicyBearer`
35+
* @returns {Builder<Specification.AuthenticationPolicyBearer>} A builder for `Specification.AuthenticationPolicyBearer`
3736
*/
38-
export const authenticationPolicyBearerBuilder = (): Builder<Specification.AuthenticationPolicyBearer> =>
39-
builder<Specification.AuthenticationPolicyBearer>(buildingFn);
37+
export const authenticationPolicyBearerBuilder = (
38+
model?: Partial<Specification.AuthenticationPolicyBearer>,
39+
): Builder<Specification.AuthenticationPolicyBearer> =>
40+
builder<Specification.AuthenticationPolicyBearer>(model, buildingFn);

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.AuthenticationPolicy} data The underlying object
24+
* @param {Specification.AuthenticationPolicy} model The underlying object
2525
* @returns {Specification.AuthenticationPolicy} The validated underlying object
2626
*/
27-
function buildingFn(data: Specification.AuthenticationPolicy): Specification.AuthenticationPolicy {
28-
const model = new Classes.AuthenticationPolicy(data);
29-
30-
validate('AuthenticationPolicy', model);
31-
return model as Specification.AuthenticationPolicy;
27+
function buildingFn(model: Specification.AuthenticationPolicy): Specification.AuthenticationPolicy {
28+
const instance = new Classes.AuthenticationPolicy(model);
29+
validate('AuthenticationPolicy', instance);
30+
return instance as Specification.AuthenticationPolicy;
3231
}
3332

3433
/**
3534
* A factory to create a builder proxy for the type `Specification.AuthenticationPolicy`
36-
* @returns {Specification.AuthenticationPolicy} A builder for `Specification.AuthenticationPolicy`
35+
* @returns {Builder<Specification.AuthenticationPolicy>} A builder for `Specification.AuthenticationPolicy`
3736
*/
38-
export const authenticationPolicyBuilder = (): Builder<Specification.AuthenticationPolicy> =>
39-
builder<Specification.AuthenticationPolicy>(buildingFn);
37+
export const authenticationPolicyBuilder = (
38+
model?: Partial<Specification.AuthenticationPolicy>,
39+
): Builder<Specification.AuthenticationPolicy> => builder<Specification.AuthenticationPolicy>(model, buildingFn);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.AuthenticationPolicyOauth2} data The underlying object
24+
* @param {Specification.AuthenticationPolicyOauth2} model The underlying object
2525
* @returns {Specification.AuthenticationPolicyOauth2} The validated underlying object
2626
*/
27-
function buildingFn(data: Specification.AuthenticationPolicyOauth2): Specification.AuthenticationPolicyOauth2 {
28-
const model = new Classes.AuthenticationPolicyOauth2(data);
29-
30-
validate('AuthenticationPolicyOauth2', model);
31-
return model as Specification.AuthenticationPolicyOauth2;
27+
function buildingFn(model: Specification.AuthenticationPolicyOauth2): Specification.AuthenticationPolicyOauth2 {
28+
const instance = new Classes.AuthenticationPolicyOauth2(model);
29+
validate('AuthenticationPolicyOauth2', instance);
30+
return instance as Specification.AuthenticationPolicyOauth2;
3231
}
3332

3433
/**
3534
* A factory to create a builder proxy for the type `Specification.AuthenticationPolicyOauth2`
36-
* @returns {Specification.AuthenticationPolicyOauth2} A builder for `Specification.AuthenticationPolicyOauth2`
35+
* @returns {Builder<Specification.AuthenticationPolicyOauth2>} A builder for `Specification.AuthenticationPolicyOauth2`
3736
*/
38-
export const authenticationPolicyOauth2Builder = (): Builder<Specification.AuthenticationPolicyOauth2> =>
39-
builder<Specification.AuthenticationPolicyOauth2>(buildingFn);
37+
export const authenticationPolicyOauth2Builder = (
38+
model?: Partial<Specification.AuthenticationPolicyOauth2>,
39+
): Builder<Specification.AuthenticationPolicyOauth2> =>
40+
builder<Specification.AuthenticationPolicyOauth2>(model, buildingFn);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.AuthenticationPolicyOauth2Client} data The underlying object
24+
* @param {Specification.AuthenticationPolicyOauth2Client} model The underlying object
2525
* @returns {Specification.AuthenticationPolicyOauth2Client} The validated underlying object
2626
*/
2727
function buildingFn(
28-
data: Specification.AuthenticationPolicyOauth2Client,
28+
model: Specification.AuthenticationPolicyOauth2Client,
2929
): Specification.AuthenticationPolicyOauth2Client {
30-
const model = new Classes.AuthenticationPolicyOauth2Client(data);
31-
32-
validate('AuthenticationPolicyOauth2Client', model);
33-
return model as Specification.AuthenticationPolicyOauth2Client;
30+
const instance = new Classes.AuthenticationPolicyOauth2Client(model);
31+
validate('AuthenticationPolicyOauth2Client', instance);
32+
return instance as Specification.AuthenticationPolicyOauth2Client;
3433
}
3534

3635
/**
3736
* A factory to create a builder proxy for the type `Specification.AuthenticationPolicyOauth2Client`
38-
* @returns {Specification.AuthenticationPolicyOauth2Client} A builder for `Specification.AuthenticationPolicyOauth2Client`
37+
* @returns {Builder<Specification.AuthenticationPolicyOauth2Client>} A builder for `Specification.AuthenticationPolicyOauth2Client`
3938
*/
40-
export const authenticationPolicyOauth2ClientBuilder = (): Builder<Specification.AuthenticationPolicyOauth2Client> =>
41-
builder<Specification.AuthenticationPolicyOauth2Client>(buildingFn);
39+
export const authenticationPolicyOauth2ClientBuilder = (
40+
model?: Partial<Specification.AuthenticationPolicyOauth2Client>,
41+
): Builder<Specification.AuthenticationPolicyOauth2Client> =>
42+
builder<Specification.AuthenticationPolicyOauth2Client>(model, buildingFn);

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.CallAsyncAPI} data The underlying object
24+
* @param {Specification.CallAsyncAPI} model The underlying object
2525
* @returns {Specification.CallAsyncAPI} The validated underlying object
2626
*/
27-
function buildingFn(data: Specification.CallAsyncAPI): Specification.CallAsyncAPI {
28-
const model = new Classes.CallAsyncAPI(data);
29-
30-
validate('CallAsyncAPI', model);
31-
return model as Specification.CallAsyncAPI;
27+
function buildingFn(model: Specification.CallAsyncAPI): Specification.CallAsyncAPI {
28+
const instance = new Classes.CallAsyncAPI(model);
29+
validate('CallAsyncAPI', instance);
30+
return instance as Specification.CallAsyncAPI;
3231
}
3332

3433
/**
3534
* A factory to create a builder proxy for the type `Specification.CallAsyncAPI`
36-
* @returns {Specification.CallAsyncAPI} A builder for `Specification.CallAsyncAPI`
35+
* @returns {Builder<Specification.CallAsyncAPI>} A builder for `Specification.CallAsyncAPI`
3736
*/
38-
export const callAsyncAPIBuilder = (): Builder<Specification.CallAsyncAPI> =>
39-
builder<Specification.CallAsyncAPI>(buildingFn);
37+
export const callAsyncAPIBuilder = (model?: Partial<Specification.CallAsyncAPI>): Builder<Specification.CallAsyncAPI> =>
38+
builder<Specification.CallAsyncAPI>(model, buildingFn);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.CallAsyncAPIWithAuthentication} data The underlying object
24+
* @param {Specification.CallAsyncAPIWithAuthentication} model The underlying object
2525
* @returns {Specification.CallAsyncAPIWithAuthentication} The validated underlying object
2626
*/
27-
function buildingFn(data: Specification.CallAsyncAPIWithAuthentication): Specification.CallAsyncAPIWithAuthentication {
28-
const model = new Classes.CallAsyncAPIWithAuthentication(data);
29-
30-
validate('CallAsyncAPIWithAuthentication', model);
31-
return model as Specification.CallAsyncAPIWithAuthentication;
27+
function buildingFn(model: Specification.CallAsyncAPIWithAuthentication): Specification.CallAsyncAPIWithAuthentication {
28+
const instance = new Classes.CallAsyncAPIWithAuthentication(model);
29+
validate('CallAsyncAPIWithAuthentication', instance);
30+
return instance as Specification.CallAsyncAPIWithAuthentication;
3231
}
3332

3433
/**
3534
* A factory to create a builder proxy for the type `Specification.CallAsyncAPIWithAuthentication`
36-
* @returns {Specification.CallAsyncAPIWithAuthentication} A builder for `Specification.CallAsyncAPIWithAuthentication`
35+
* @returns {Builder<Specification.CallAsyncAPIWithAuthentication>} A builder for `Specification.CallAsyncAPIWithAuthentication`
3736
*/
38-
export const callAsyncAPIWithAuthenticationBuilder = (): Builder<Specification.CallAsyncAPIWithAuthentication> =>
39-
builder<Specification.CallAsyncAPIWithAuthentication>(buildingFn);
37+
export const callAsyncAPIWithAuthenticationBuilder = (
38+
model?: Partial<Specification.CallAsyncAPIWithAuthentication>,
39+
): Builder<Specification.CallAsyncAPIWithAuthentication> =>
40+
builder<Specification.CallAsyncAPIWithAuthentication>(model, buildingFn);

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.CallAsyncAPIWith} data The underlying object
24+
* @param {Specification.CallAsyncAPIWith} model The underlying object
2525
* @returns {Specification.CallAsyncAPIWith} The validated underlying object
2626
*/
27-
function buildingFn(data: Specification.CallAsyncAPIWith): Specification.CallAsyncAPIWith {
28-
const model = new Classes.CallAsyncAPIWith(data);
29-
30-
validate('CallAsyncAPIWith', model);
31-
return model as Specification.CallAsyncAPIWith;
27+
function buildingFn(model: Specification.CallAsyncAPIWith): Specification.CallAsyncAPIWith {
28+
const instance = new Classes.CallAsyncAPIWith(model);
29+
validate('CallAsyncAPIWith', instance);
30+
return instance as Specification.CallAsyncAPIWith;
3231
}
3332

3433
/**
3534
* A factory to create a builder proxy for the type `Specification.CallAsyncAPIWith`
36-
* @returns {Specification.CallAsyncAPIWith} A builder for `Specification.CallAsyncAPIWith`
35+
* @returns {Builder<Specification.CallAsyncAPIWith>} A builder for `Specification.CallAsyncAPIWith`
3736
*/
38-
export const callAsyncAPIWithBuilder = (): Builder<Specification.CallAsyncAPIWith> =>
39-
builder<Specification.CallAsyncAPIWith>(buildingFn);
37+
export const callAsyncAPIWithBuilder = (
38+
model?: Partial<Specification.CallAsyncAPIWith>,
39+
): Builder<Specification.CallAsyncAPIWith> => builder<Specification.CallAsyncAPIWith>(model, buildingFn);

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@ import { Specification } from '../definitions';
2121

2222
/**
2323
* The internal function used by the builder proxy to validate and return its underlying object
24-
* @param {Specification.CallFunction} data The underlying object
24+
* @param {Specification.CallFunction} model The underlying object
2525
* @returns {Specification.CallFunction} The validated underlying object
2626
*/
27-
function buildingFn(data: Specification.CallFunction): Specification.CallFunction {
28-
const model = new Classes.CallFunction(data);
29-
30-
validate('CallFunction', model);
31-
return model as Specification.CallFunction;
27+
function buildingFn(model: Specification.CallFunction): Specification.CallFunction {
28+
const instance = new Classes.CallFunction(model);
29+
validate('CallFunction', instance);
30+
return instance as Specification.CallFunction;
3231
}
3332

3433
/**
3534
* A factory to create a builder proxy for the type `Specification.CallFunction`
36-
* @returns {Specification.CallFunction} A builder for `Specification.CallFunction`
35+
* @returns {Builder<Specification.CallFunction>} A builder for `Specification.CallFunction`
3736
*/
38-
export const callFunctionBuilder = (): Builder<Specification.CallFunction> =>
39-
builder<Specification.CallFunction>(buildingFn);
37+
export const callFunctionBuilder = (model?: Partial<Specification.CallFunction>): Builder<Specification.CallFunction> =>
38+
builder<Specification.CallFunction>(model, buildingFn);

0 commit comments

Comments
 (0)