Skip to content

Commit 8f7fc70

Browse files
committed
chore: add sponsors section
1 parent 0a56bcd commit 8f7fc70

File tree

12 files changed

+609
-0
lines changed

12 files changed

+609
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ export default {
7676

7777
In this example, the generator will read the AsyncAPI document located in same directory as the configuration file, and generate TypeScript models for all the payload including code to serialize the models to JSON.
7878

79+
# 💎 Sponsors
80+
81+
<div align="center">
82+
83+
<table>
84+
<tr>
85+
<td align="center" width="33%">
86+
<a href="https://code-forge.net/" target="_blank">
87+
<img src="./assets/images/sponsors/CodeForge.png" alt="CodeForge" width="200">
88+
</a>
89+
<br />
90+
<strong><a href="https://code-forge.net/" target="_blank">CodeForge</a></strong>
91+
<br />
92+
<em>Automated SDK generation platform built on The Codegen Project</em>
93+
</td>
94+
</tr>
95+
</table>
96+
97+
</div>
98+
99+
---
100+
79101
# Getting started
80102
Its simple, [install the CLI](#install) into your project or machine, [setup the Codegen configuration file](#initialize) to include all the code your heart desire, customize it, and generate it at build time or whenever you feel like it.
81103

231 KB
Loading
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import {APet} from './../payloads/APet';
2+
import * as AddPetResponseModule from './../payloads/AddPetResponse';
3+
import * as UpdatePetResponseModule from './../payloads/UpdatePetResponse';
4+
import * as FindPetsByStatusAndCategoryResponseModule from './../payloads/FindPetsByStatusAndCategoryResponse';
5+
import {PetCategory} from './../payloads/PetCategory';
6+
import {PetTag} from './../payloads/PetTag';
7+
import {Status} from './../payloads/Status';
8+
import {OneOf_0Status} from './../payloads/OneOf_0Status';
9+
import {OneOf_0ItemStatus} from './../payloads/OneOf_0ItemStatus';
10+
import {PetOrder} from './../payloads/PetOrder';
11+
import {AUser} from './../payloads/AUser';
12+
import {AnUploadedResponse} from './../payloads/AnUploadedResponse';
13+
import {FindPetsByStatusAndCategoryParameters} from './../parameters/FindPetsByStatusAndCategoryParameters';
14+
import * as NodeFetch from 'node-fetch';
15+
export const Protocols = {
16+
http_client: {
17+
async postAddPet(context: {
18+
server?: 'http://petstore.swagger.io/v2' | string;
19+
payload: APet;
20+
path?: string;
21+
headers?: Record<string, string | string[]>; // header params we want to use on every request
22+
makeRequestCallback?: ({
23+
method, body, url, headers
24+
}: {
25+
url: string,
26+
headers?: Record<string, string | string[]>,
27+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD',
28+
body?: any
29+
}) => Promise<{
30+
ok: boolean,
31+
status: number,
32+
statusText: string,
33+
json: () => Record<any, any> | Promise<Record<any, any>>,
34+
}>
35+
}): Promise<{error?: string, statusCode: number, payload?: AddPetResponseModule.AddPetResponse, rawResponse?: any, rawData?: any}> {
36+
const parsedContext = {
37+
...{
38+
makeRequestCallback: async ({url, body, method, headers}) => {
39+
return NodeFetch.default(url, {
40+
body,
41+
method,
42+
headers
43+
})
44+
},
45+
path: '/pet',
46+
server: 'http://petstore.swagger.io/v2',
47+
},
48+
...context,
49+
}
50+
51+
const headers = {
52+
'Content-Type': 'application/json',
53+
...parsedContext.headers
54+
};
55+
56+
const url = `${parsedContext.server}${parsedContext.path}`;
57+
58+
let body: any;
59+
if (parsedContext.payload) {
60+
body = parsedContext.payload.marshal();
61+
}
62+
63+
// Make the API request
64+
const response = await parsedContext.makeRequestCallback({
65+
url,
66+
method: 'POST',
67+
headers,
68+
body
69+
});
70+
71+
// For multi-status responses, always try to parse JSON and let unmarshalByStatusCode handle it
72+
try {
73+
const data = await response.json();
74+
return {...AddPetResponseModule.unmarshalByStatusCode(data, response.status), rawData: data, rawResponse: response, statusCode: response.status};
75+
} catch (error) {
76+
return {error: `Error parsing JSON response: ${error}`, statusCode: response.status, rawResponse: response};
77+
}
78+
},
79+
async putUpdatePet(context: {
80+
server?: 'http://petstore.swagger.io/v2' | string;
81+
payload: APet;
82+
path?: string;
83+
headers?: Record<string, string | string[]>; // header params we want to use on every request
84+
makeRequestCallback?: ({
85+
method, body, url, headers
86+
}: {
87+
url: string,
88+
headers?: Record<string, string | string[]>,
89+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD',
90+
body?: any
91+
}) => Promise<{
92+
ok: boolean,
93+
status: number,
94+
statusText: string,
95+
json: () => Record<any, any> | Promise<Record<any, any>>,
96+
}>
97+
}): Promise<{error?: string, statusCode: number, payload?: UpdatePetResponseModule.UpdatePetResponse, rawResponse?: any, rawData?: any}> {
98+
const parsedContext = {
99+
...{
100+
makeRequestCallback: async ({url, body, method, headers}) => {
101+
return NodeFetch.default(url, {
102+
body,
103+
method,
104+
headers
105+
})
106+
},
107+
path: '/pet',
108+
server: 'http://petstore.swagger.io/v2',
109+
},
110+
...context,
111+
}
112+
113+
const headers = {
114+
'Content-Type': 'application/json',
115+
...parsedContext.headers
116+
};
117+
118+
const url = `${parsedContext.server}${parsedContext.path}`;
119+
120+
let body: any;
121+
if (parsedContext.payload) {
122+
body = parsedContext.payload.marshal();
123+
}
124+
125+
// Make the API request
126+
const response = await parsedContext.makeRequestCallback({
127+
url,
128+
method: 'PUT',
129+
headers,
130+
body
131+
});
132+
133+
// For multi-status responses, always try to parse JSON and let unmarshalByStatusCode handle it
134+
try {
135+
const data = await response.json();
136+
return {...UpdatePetResponseModule.unmarshalByStatusCode(data, response.status), rawData: data, rawResponse: response, statusCode: response.status};
137+
} catch (error) {
138+
return {error: `Error parsing JSON response: ${error}`, statusCode: response.status, rawResponse: response};
139+
}
140+
},
141+
async getFindPetsByStatusAndCategory(context: {
142+
server?: 'http://petstore.swagger.io/v2' | string;
143+
144+
path?: string;
145+
headers?: Record<string, string | string[]>; // header params we want to use on every request
146+
makeRequestCallback?: ({
147+
method, body, url, headers
148+
}: {
149+
url: string,
150+
headers?: Record<string, string | string[]>,
151+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD',
152+
body?: any
153+
}) => Promise<{
154+
ok: boolean,
155+
status: number,
156+
statusText: string,
157+
json: () => Record<any, any> | Promise<Record<any, any>>,
158+
}>
159+
}): Promise<{error?: string, statusCode: number, payload?: FindPetsByStatusAndCategoryResponseModule.FindPetsByStatusAndCategoryResponse, rawResponse?: any, rawData?: any}> {
160+
const parsedContext = {
161+
...{
162+
makeRequestCallback: async ({url, body, method, headers}) => {
163+
return NodeFetch.default(url, {
164+
body,
165+
method,
166+
headers
167+
})
168+
},
169+
path: '/pet/findByStatus/{status}/{categoryId}',
170+
server: 'http://petstore.swagger.io/v2',
171+
},
172+
...context,
173+
}
174+
175+
const headers = {
176+
'Content-Type': 'application/json',
177+
...parsedContext.headers
178+
};
179+
180+
const url = `${parsedContext.server}${parsedContext.path}`;
181+
182+
let body: any;
183+
184+
185+
// Make the API request
186+
const response = await parsedContext.makeRequestCallback({
187+
url,
188+
method: 'GET',
189+
headers,
190+
body
191+
});
192+
193+
// For multi-status responses, always try to parse JSON and let unmarshalByStatusCode handle it
194+
try {
195+
const data = await response.json();
196+
return {...FindPetsByStatusAndCategoryResponseModule.unmarshalByStatusCode(data, response.status), rawData: data, rawResponse: response, statusCode: response.status};
197+
} catch (error) {
198+
return {error: `Error parsing JSON response: ${error}`, statusCode: response.status, rawResponse: response};
199+
}
200+
}
201+
}};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {APet} from './APet';
2+
import {OneOf_0Status} from './OneOf_0Status';
3+
import {Ajv, Options as AjvOptions, ErrorObject, ValidateFunction} from 'ajv';
4+
import addFormats from 'ajv-formats';
5+
type AddPetResponse = APet | string;
6+
7+
export function unmarshal(json: any): AddPetResponse {
8+
const parsed = typeof json === 'string' ? JSON.parse(json) : json;
9+
if(json.status === OneOf_0Status.AVAILABLE) {
10+
return APet.unmarshal(json);
11+
}
12+
if(typeof parsed === 'object' && parsed !== null) {
13+
if(parsed.status === OneOf_0Status.AVAILABLE) {
14+
return APet.unmarshal(json);
15+
}
16+
}
17+
return parsed;
18+
}
19+
export function marshal(payload: AddPetResponse): string {
20+
if(payload instanceof APet) {
21+
return payload.marshal();
22+
}
23+
return JSON.stringify(payload);
24+
}
25+
export function unmarshalByStatusCode(json: any, statusCode: number): {error?: string, statusCode: number, payload?: AddPetResponse} {
26+
switch(statusCode) {
27+
case 200:
28+
return {statusCode, payload: APet.unmarshal(json)};
29+
case 405:
30+
return {statusCode, payload: JSON.parse(json) as string};
31+
default:
32+
return {error: `No matching type found for status code: ${statusCode}`, statusCode};
33+
}
34+
}
35+
export const theCodeGenSchema = {"type":"object","oneOf":[{"title":"a Pet","description":"A pet for sale in the pet store","type":"object","required":["name","photoUrls"],"properties":{"id":{"type":"integer","format":"int64"},"category":{"title":"Pet category","description":"A category for a pet","type":"object","properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string","pattern":"^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$"}},"xml":{"name":"Category"}},"name":{"type":"string","example":"doggie"},"photoUrls":{"type":"array","xml":{"name":"photoUrl","wrapped":true},"items":{"type":"string"}},"tags":{"type":"array","xml":{"name":"tag","wrapped":true},"items":{"title":"Pet Tag","description":"A tag for a pet","type":"object","properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"}},"xml":{"name":"Tag"}}},"status":{"type":"string","description":"pet status in the store","deprecated":true,"enum":["available","pending","sold"]}},"xml":{"name":"Pet"},"$id":"addPet_Response_200"},{"type":"string","description":"Invalid input","$id":"addPet_Response_405"}],"$id":"AddPetResponse","$schema":"http://json-schema.org/draft-07/schema"};
36+
export function validate(context?: {data: any, ajvValidatorFunction?: ValidateFunction, ajvInstance?: Ajv, ajvOptions?: AjvOptions}): { valid: boolean; errors?: ErrorObject[]; } {
37+
const {data, ajvValidatorFunction} = context ?? {};
38+
const parsedData = typeof data === 'string' ? JSON.parse(data) : data;
39+
const validate = ajvValidatorFunction ?? createValidator(context)
40+
return {
41+
valid: validate(parsedData),
42+
errors: validate.errors ?? undefined,
43+
};
44+
}
45+
export function createValidator(context?: {ajvInstance?: Ajv, ajvOptions?: AjvOptions}): ValidateFunction {
46+
const {ajvInstance} = {...context ?? {}, ajvInstance: new Ajv(context?.ajvOptions ?? {})};
47+
addFormats(ajvInstance);
48+
ajvInstance.addVocabulary(["xml", "example"])
49+
const validate = ajvInstance.compile(theCodeGenSchema);
50+
return validate;
51+
}
52+
53+
54+
export { AddPetResponse };
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {APet} from './APet';
2+
import {Ajv, Options as AjvOptions, ErrorObject, ValidateFunction} from 'ajv';
3+
import addFormats from 'ajv-formats';
4+
type FindPetsByStatusAndCategoryResponse = APet[] | string | string;
5+
6+
export function unmarshal(json: any): FindPetsByStatusAndCategoryResponse {
7+
const parsed = typeof json === 'string' ? JSON.parse(json) : json;
8+
if(Array.isArray(parsed)) {
9+
const unmarshalledArray = parsed.map(item => {
10+
if (item) {
11+
return APet.unmarshal(item);
12+
}
13+
return item;
14+
});
15+
return unmarshalledArray;
16+
}
17+
18+
return parsed;
19+
}
20+
export function marshal(payload: FindPetsByStatusAndCategoryResponse): string {
21+
if(payload instanceof Array) {
22+
return JSON.stringify(payload.map(item => {
23+
if(item && typeof item === 'object' && typeof item.marshal === 'function') {
24+
return item.marshal();
25+
}
26+
return item;
27+
}));
28+
}
29+
return JSON.stringify(payload);
30+
}
31+
export function unmarshalByStatusCode(json: any, statusCode: number): {error?: string, statusCode: number, payload?: FindPetsByStatusAndCategoryResponse} {
32+
switch(statusCode) {
33+
case 200:
34+
const parsed = typeof json === 'string' ? JSON.parse(json) : json;
35+
if (!Array.isArray(parsed)) {
36+
throw new Error('Expected array');
37+
}
38+
const unmarshalledArray = parsed.map(item => {
39+
if (item) {
40+
return APet.unmarshal(item);
41+
}
42+
return item;
43+
});
44+
return {statusCode, payload: unmarshalledArray};
45+
case 400:
46+
return {statusCode, payload: JSON.parse(json) as string};
47+
case 404:
48+
return {statusCode, payload: JSON.parse(json) as string};
49+
default:
50+
return {error: `No matching type found for status code: ${statusCode}`, statusCode};
51+
}
52+
}
53+
export const theCodeGenSchema = {"type":"object","oneOf":[{"type":"array","items":{"title":"a Pet","description":"A pet for sale in the pet store","type":"object","required":["name","photoUrls"],"properties":{"id":{"type":"integer","format":"int64"},"category":{"title":"Pet category","description":"A category for a pet","type":"object","properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string","pattern":"^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$"}},"xml":{"name":"Category"}},"name":{"type":"string","example":"doggie"},"photoUrls":{"type":"array","xml":{"name":"photoUrl","wrapped":true},"items":{"type":"string"}},"tags":{"type":"array","xml":{"name":"tag","wrapped":true},"items":{"title":"Pet Tag","description":"A tag for a pet","type":"object","properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"}},"xml":{"name":"Tag"}}},"status":{"type":"string","description":"pet status in the store","deprecated":true,"enum":["available","pending","sold"]}},"xml":{"name":"Pet"}},"$id":"findPetsByStatusAndCategory_Response_200"},{"type":"string","description":"Invalid status value or category ID","$id":"findPetsByStatusAndCategory_Response_400"},{"type":"string","description":"Category not found","$id":"findPetsByStatusAndCategory_Response_404"}],"$id":"FindPetsByStatusAndCategoryResponse","$schema":"http://json-schema.org/draft-07/schema"};
54+
export function validate(context?: {data: any, ajvValidatorFunction?: ValidateFunction, ajvInstance?: Ajv, ajvOptions?: AjvOptions}): { valid: boolean; errors?: ErrorObject[]; } {
55+
const {data, ajvValidatorFunction} = context ?? {};
56+
const parsedData = typeof data === 'string' ? JSON.parse(data) : data;
57+
const validate = ajvValidatorFunction ?? createValidator(context)
58+
return {
59+
valid: validate(parsedData),
60+
errors: validate.errors ?? undefined,
61+
};
62+
}
63+
export function createValidator(context?: {ajvInstance?: Ajv, ajvOptions?: AjvOptions}): ValidateFunction {
64+
const {ajvInstance} = {...context ?? {}, ajvInstance: new Ajv(context?.ajvOptions ?? {})};
65+
addFormats(ajvInstance);
66+
ajvInstance.addVocabulary(["xml", "example"])
67+
const validate = ajvInstance.compile(theCodeGenSchema);
68+
return validate;
69+
}
70+
71+
72+
export { FindPetsByStatusAndCategoryResponse };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
enum OneOf_0ItemStatus {
3+
AVAILABLE = "available",
4+
PENDING = "pending",
5+
SOLD = "sold",
6+
}
7+
export { OneOf_0ItemStatus };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
enum OneOf_0Status {
3+
AVAILABLE = "available",
4+
PENDING = "pending",
5+
SOLD = "sold",
6+
}
7+
export { OneOf_0Status };

0 commit comments

Comments
 (0)