Skip to content

Commit bdd47ec

Browse files
committed
Updated readme for DTO code generation
Added openApi codegen templates
1 parent 5e50cde commit bdd47ec

File tree

11 files changed

+755
-0
lines changed

11 files changed

+755
-0
lines changed

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,96 @@ Important versions listed below. Refer to the [Changelog](CHANGELOG.md) for a fu
7878
- [0.10.1-beta](CHANGELOG.md#v0101-beta) - **Alpaca compatible** 07.2018
7979
- [0.9.5](CHANGELOG.md#095-27-Jun-2018) - **Alpaca compatible** 07.2018
8080

81+
## Notes on generation of catapult-rest DTO and API client
82+
83+
Following command can be used to generate DTOs and Api clients for the [nem2-sdk-typescript-javascript](https://github.com/nemtech/nem2-sdk-typescript-javascript) :
84+
85+
1.
86+
```bash
87+
$ git clone git@github.com:nemtech/nem2-docs
88+
$ cd nem2-docs && mkdir sdks && cd sdks
89+
$ cp ../source/resources/collections/swagger.yaml .
90+
```
91+
2. Copy the `templates` folder from {nem2-sdk-typescript-javascript}/infrustructure/ into `sdk` folder
92+
93+
3.
94+
```bash
95+
$ brew install openapi-generator
96+
$ openapi-generator generate -i ./swagger2.yaml -g typescript-node -t templates/ -o ./nem2-ts-sdk/ && rm -R nem2-ts-sdk/test
97+
```
98+
4. As the generator doesn't recognize `enum` type alias, we need to manually move enum classes in to the `enumsMap` list.
99+
- Open generated file `./nem2-ts-sdk/model/models.ts` in editor
100+
- Search for line contains `let enumsMap: {[index: string]: any}`.
101+
- Move all `xxxTypeEnum` entries from below `typeMap` into `enumsMap`.
102+
103+
example below:
104+
```js
105+
let enumsMap: {[index: string]: any} = {
106+
"AccountPropertyTypeEnum": AccountPropertyTypeEnum,
107+
"AliasTypeEnum": AliasTypeEnum,
108+
"ResolutionStatementDTO": ResolutionStatementDTO,
109+
"MosaicPropertyIdEnum": MosaicPropertyIdEnum,
110+
"MultisigModificationTypeEnum": MultisigModificationTypeEnum,
111+
"NamespaceTypeEnum": NamespaceTypeEnum,
112+
"ReceiptTypeEnum": ReceiptTypeEnum,
113+
"RolesTypeEnum": RolesTypeEnum,
114+
}
115+
116+
let typeMap: {[index: string]: any} = {
117+
"AccountDTO": AccountDTO,
118+
"AccountIds": AccountIds,
119+
"AccountInfoDTO": AccountInfoDTO,
120+
"AccountMetaDTO": AccountMetaDTO,
121+
"AccountNamesDTO": AccountNamesDTO,
122+
"AccountPropertiesDTO": AccountPropertiesDTO,
123+
"AccountPropertiesInfoDTO": AccountPropertiesInfoDTO,
124+
"AccountPropertyDTO": AccountPropertyDTO,
125+
"AliasDTO": AliasDTO,
126+
"AnnounceTransactionInfoDTO": AnnounceTransactionInfoDTO,
127+
"BlockDTO": BlockDTO,
128+
"BlockInfoDTO": BlockInfoDTO,
129+
"BlockMetaDTO": BlockMetaDTO,
130+
"BlockchainScoreDTO": BlockchainScoreDTO,
131+
"CommunicationTimestamps": CommunicationTimestamps,
132+
"Cosignature": Cosignature,
133+
"HeightInfoDTO": HeightInfoDTO,
134+
"MerklePathItem": MerklePathItem,
135+
"MerkleProofInfo": MerkleProofInfo,
136+
"MerkleProofInfoDTO": MerkleProofInfoDTO,
137+
"MosaicDTO": MosaicDTO,
138+
"MosaicDefinitionDTO": MosaicDefinitionDTO,
139+
"MosaicIds": MosaicIds,
140+
"MosaicInfoDTO": MosaicInfoDTO,
141+
"MosaicMetaDTO": MosaicMetaDTO,
142+
"MosaicNamesDTO": MosaicNamesDTO,
143+
"MosaicPropertyDTO": MosaicPropertyDTO,
144+
"MultisigAccountGraphInfoDTO": MultisigAccountGraphInfoDTO,
145+
"MultisigAccountInfoDTO": MultisigAccountInfoDTO,
146+
"MultisigDTO": MultisigDTO,
147+
"NamespaceDTO": NamespaceDTO,
148+
"NamespaceIds": NamespaceIds,
149+
"NamespaceInfoDTO": NamespaceInfoDTO,
150+
"NamespaceMetaDTO": NamespaceMetaDTO,
151+
"NamespaceNameDTO": NamespaceNameDTO,
152+
"NetworkTypeDTO": NetworkTypeDTO,
153+
"NodeInfoDTO": NodeInfoDTO,
154+
"NodeTimeDTO": NodeTimeDTO,
155+
"ResolutionEntryDTO": ResolutionEntryDTO,
156+
"ServerDTO": ServerDTO,
157+
"ServerInfoDTO": ServerInfoDTO,
158+
"SourceDTO": SourceDTO,
159+
"StatementsDTO": StatementsDTO,
160+
"StorageInfoDTO": StorageInfoDTO,
161+
"TransactionHashes": TransactionHashes,
162+
"TransactionIds": TransactionIds,
163+
"TransactionInfoDTO": TransactionInfoDTO,
164+
"TransactionMetaDTO": TransactionMetaDTO,
165+
"TransactionPayload": TransactionPayload,
166+
"TransactionStatementDTO": TransactionStatementDTO,
167+
"TransactionStatusDTO": TransactionStatusDTO,
168+
}
169+
```
170+
81171
## License
82172
83173
Copyright (c) 2018-2019 NEM
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{>licenseInfo}}
2+
{{#apiInfo}}
3+
{{#apis}}
4+
{{#operations}}
5+
export * from './{{ classFilename }}';
6+
import { {{ classname }} } from './{{ classFilename }}';
7+
{{/operations}}
8+
{{#withInterfaces}}
9+
export * from './{{ classFilename }}Interface'
10+
{{/withInterfaces}}
11+
{{/apis}}
12+
export const APIS = [{{#apis}}{{#operations}}{{ classname }}{{/operations}}{{^-last}}, {{/-last}}{{/apis}}];
13+
{{/apiInfo}}
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
{{>licenseInfo}}
2+
import localVarRequest = require('request');
3+
import http = require('http');
4+
5+
/* tslint:disable:no-unused-locals */
6+
{{#imports}}
7+
import { {{classname}} } from '../{{filename}}';
8+
{{/imports}}
9+
10+
import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
11+
{{#hasAuthMethods}}
12+
{{#authMethods}}
13+
{{#isBasic}}
14+
import { HttpBasicAuth } from '../model/models';
15+
{{/isBasic}}
16+
{{#isApiKey}}
17+
import { ApiKeyAuth } from '../model/models';
18+
{{/isApiKey}}
19+
{{#isOAuth}}
20+
import { OAuth } from '../model/models';
21+
{{/isOAuth}}
22+
{{/authMethods}}
23+
{{/hasAuthMethods}}
24+
25+
let defaultBasePath = '{{{basePath}}}';
26+
27+
// ===============================================
28+
// This file is autogenerated - Please do not edit
29+
// ===============================================
30+
31+
{{#operations}}
32+
{{#description}}
33+
/**
34+
* {{&description}}
35+
*/
36+
{{/description}}
37+
export enum {{classname}}ApiKeys {
38+
{{#authMethods}}
39+
{{#isApiKey}}
40+
{{name}},
41+
{{/isApiKey}}
42+
{{/authMethods}}
43+
}
44+
45+
export class {{classname}} {
46+
protected _basePath = defaultBasePath;
47+
protected defaultHeaders : any = {};
48+
protected _useQuerystring : boolean = false;
49+
50+
protected authentications = {
51+
'default': <Authentication>new VoidAuth(),
52+
{{#hasAuthMethods}}
53+
{{#authMethods}}
54+
{{#isBasic}}
55+
'{{name}}': new HttpBasicAuth(),
56+
{{/isBasic}}
57+
{{#isApiKey}}
58+
'{{name}}': new ApiKeyAuth({{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{^isKeyInHeader}}'query'{{/isKeyInHeader}}, '{{keyParamName}}'),
59+
{{/isApiKey}}
60+
{{#isOAuth}}
61+
'{{name}}': new OAuth(),
62+
{{/isOAuth}}
63+
{{/authMethods}}
64+
{{/hasAuthMethods}}
65+
}
66+
67+
constructor(basePath?: string);
68+
{{#authMethods}}
69+
{{#isBasic}}
70+
constructor(username: string, password: string, basePath?: string);
71+
{{/isBasic}}
72+
{{/authMethods}}
73+
constructor(basePathOrUsername: string, password?: string, basePath?: string) {
74+
if (password) {
75+
{{#authMethods}}
76+
{{#isBasic}}
77+
this.username = basePathOrUsername;
78+
this.password = password
79+
{{/isBasic}}
80+
{{/authMethods}}
81+
if (basePath) {
82+
this.basePath = basePath;
83+
}
84+
} else {
85+
if (basePathOrUsername) {
86+
this.basePath = basePathOrUsername
87+
}
88+
}
89+
}
90+
91+
set useQuerystring(value: boolean) {
92+
this._useQuerystring = value;
93+
}
94+
95+
set basePath(basePath: string) {
96+
this._basePath = basePath;
97+
}
98+
99+
get basePath() {
100+
return this._basePath;
101+
}
102+
103+
public setDefaultAuthentication(auth: Authentication) {
104+
this.authentications.default = auth;
105+
}
106+
107+
public setApiKey(key: {{classname}}ApiKeys, value: string) {
108+
(this.authentications as any)[{{classname}}ApiKeys[key]].apiKey = value;
109+
}
110+
{{#hasAuthMethods}}
111+
{{#authMethods}}
112+
{{#isBasic}}
113+
set username(username: string) {
114+
this.authentications.{{name}}.username = username;
115+
}
116+
117+
set password(password: string) {
118+
this.authentications.{{name}}.password = password;
119+
}
120+
{{/isBasic}}
121+
{{#isOAuth}}
122+
123+
set accessToken(token: string) {
124+
this.authentications.{{name}}.accessToken = token;
125+
}
126+
{{/isOAuth}}
127+
{{/authMethods}}
128+
{{/hasAuthMethods}}
129+
130+
{{#operation}}
131+
/**
132+
* {{&notes}}
133+
{{#summary}}
134+
* @summary {{&summary}}
135+
{{/summary}}
136+
{{#allParams}}
137+
* @param {{paramName}} {{description}}
138+
{{/allParams}}
139+
*/
140+
public async {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.{{#supportsES6}}IncomingMessage{{/supportsES6}}{{^supportsES6}}ClientResponse{{/supportsES6}}; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
141+
const localVarPath = this.basePath + '{{{path}}}'{{#pathParams}}
142+
.replace('{' + '{{baseName}}' + '}', encodeURIComponent(String({{paramName}}))){{/pathParams}};
143+
let localVarQueryParameters: any = {};
144+
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
145+
let localVarFormParams: any = {};
146+
147+
{{#allParams}}
148+
{{#required}}
149+
// verify required parameter '{{paramName}}' is not null or undefined
150+
if ({{paramName}} === null || {{paramName}} === undefined) {
151+
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
152+
}
153+
154+
{{/required}}
155+
{{/allParams}}
156+
{{#queryParams}}
157+
if ({{paramName}} !== undefined) {
158+
localVarQueryParameters['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
159+
}
160+
161+
{{/queryParams}}
162+
{{#headerParams}}
163+
localVarHeaderParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
164+
{{/headerParams}}
165+
(<any>Object).assign(localVarHeaderParams, options.headers);
166+
167+
let localVarUseFormData = false;
168+
169+
{{#formParams}}
170+
if ({{paramName}} !== undefined) {
171+
{{#isFile}}
172+
localVarFormParams['{{baseName}}'] = {{paramName}};
173+
{{/isFile}}
174+
{{^isFile}}
175+
localVarFormParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
176+
{{/isFile}}
177+
}
178+
{{#isFile}}
179+
localVarUseFormData = true;
180+
{{/isFile}}
181+
182+
{{/formParams}}
183+
let localVarRequestOptions: localVarRequest.Options = {
184+
method: '{{httpMethod}}',
185+
qs: localVarQueryParameters,
186+
headers: localVarHeaderParams,
187+
uri: localVarPath,
188+
useQuerystring: this._useQuerystring,
189+
{{^isResponseFile}}
190+
json: true,
191+
{{/isResponseFile}}
192+
{{#isResponseFile}}
193+
encoding: null,
194+
{{/isResponseFile}}
195+
{{#bodyParam}}
196+
body: ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}")
197+
{{/bodyParam}}
198+
};
199+
200+
{{#authMethods}}
201+
this.authentications.{{name}}.applyToRequest(localVarRequestOptions);
202+
203+
{{/authMethods}}
204+
this.authentications.default.applyToRequest(localVarRequestOptions);
205+
206+
if (Object.keys(localVarFormParams).length) {
207+
if (localVarUseFormData) {
208+
(<any>localVarRequestOptions).formData = localVarFormParams;
209+
} else {
210+
localVarRequestOptions.form = localVarFormParams;
211+
}
212+
}
213+
return new Promise<{ response: http.{{#supportsES6}}IncomingMessage{{/supportsES6}}{{^supportsES6}}ClientResponse{{/supportsES6}}; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>((resolve, reject) => {
214+
localVarRequest(localVarRequestOptions, (error, response, body) => {
215+
if (error) {
216+
reject(error);
217+
} else {
218+
{{#returnType}}
219+
body = ObjectSerializer.deserialize(body, "{{{returnType}}}");
220+
{{/returnType}}
221+
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
222+
resolve(body);
223+
} else {
224+
reject({
225+
statusCode: response.statusCode,
226+
statusMessage: response.statusMessage
227+
});
228+
}
229+
}
230+
});
231+
});
232+
}
233+
{{/operation}}
234+
}
235+
{{/operations}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{>licenseInfo}}
2+
// This is the entrypoint for the package
3+
export * from './api/apis';
4+
export * from './model/models';

0 commit comments

Comments
 (0)