mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-04 15:26:10 +00:00
Support language N4JS (#15089)
* n4js initial commit * incorporate feedback from user * add tests * fix media type in case of DELETE method * fix media type * some minor fixes * options fix for booleans * small fixes * generated files by ./bin/utils/ensure-up-to-date * remove String::toLowerCase due to de.thetaphi:forbiddenapis * adjust test expectation * fix test expectations * fix test expectation * add note to section 'Languages/Generators' * remove file according to review
This commit is contained in:
113
samples/client/petstore/n4js/api/ApiHelper.n4js
Normal file
113
samples/client/petstore/n4js/api/ApiHelper.n4js
Normal file
@@ -0,0 +1,113 @@
|
||||
|
||||
/**
|
||||
* Implemented by client
|
||||
*/
|
||||
export public interface ~ApiExecuterI {
|
||||
public <R, E> async exec(
|
||||
method: string,
|
||||
path: string,
|
||||
pathParams: ~Object+,
|
||||
queryParams: ~Object+,
|
||||
headerParams: ~Object+,
|
||||
payloadContentType: string,
|
||||
body: any+) : Promise<R, Object|ApiError<E>>;
|
||||
}
|
||||
|
||||
export public interface ~ApiError<T> {
|
||||
public resultBody?: T;
|
||||
}
|
||||
|
||||
export public function checkRequiredParams(apiName: string, params: ~Object+) : void {
|
||||
for (const key of Object.keys(params)) {
|
||||
const arg = params[key];
|
||||
if (arg == null) {
|
||||
throw new Error('Required parameter ' + key + ' was null or undefined when calling ' + apiName + '.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export public function <T> cleanCopyBody(t : T+, ...properties: string) : ~T {
|
||||
const copy : ~T+ = {};
|
||||
for (const prop in properties) {
|
||||
copy[prop] = t.prop;
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation of ApiExecuterI
|
||||
*
|
||||
* The following dependencies are necessary:
|
||||
* - n4js-runtime-esnext
|
||||
* - n4js-runtime-es2015
|
||||
* - n4js-runtime-html5
|
||||
*/
|
||||
export public class FetchApiExec implements ApiExecuterI {
|
||||
public apiOrigin: string;
|
||||
const jsonTypes = ["application/json", "application/problem+json"];
|
||||
|
||||
@Override
|
||||
public <R, E> async exec(
|
||||
method: string,
|
||||
path: string,
|
||||
pathParams: ~Object+,
|
||||
queryParams: ~Object+,
|
||||
headerParams: ~Object+,
|
||||
payloadContentType: string,
|
||||
body: any+
|
||||
): Promise<R, Object|ApiError<E>> {
|
||||
|
||||
if (pathParams) {
|
||||
for (const [k, v] of Object.entries(pathParams)) {
|
||||
path = path.replace(`{${k}}`, encodeURIComponent(String(v)));
|
||||
}
|
||||
}
|
||||
const query: string[] = [];
|
||||
if (queryParams) {
|
||||
for (const [k, v] of Object.entries(queryParams)) {
|
||||
query.push(`${k}=${encodeURIComponent(String(v))}`);
|
||||
}
|
||||
}
|
||||
|
||||
let url = `${this.apiOrigin}${path}`;
|
||||
if (query.length) {
|
||||
url += `?${query.join("&")}`;
|
||||
}
|
||||
|
||||
const headers: Object+ = {};
|
||||
if (payloadContentType) {
|
||||
headers["content-type"] = payloadContentType;
|
||||
if (this.constructor.jsonTypes.includes(payloadContentType)) {
|
||||
body = JSON.stringify(body);
|
||||
}
|
||||
}
|
||||
Object.assign(headers, headerParams);
|
||||
|
||||
return await this.<R,E>fetchExec(url, {
|
||||
method,
|
||||
headers,
|
||||
body,
|
||||
});
|
||||
}
|
||||
|
||||
protected <R, E> async fetchExec(url: string, reqInit: RequestInit): Promise<R, Object|ApiError<E>> {
|
||||
const resp = await fetch(url, reqInit);
|
||||
|
||||
if (resp.status !== 204) {
|
||||
const contentType = (resp.headers.get("content-type") || "").split(";")[0];
|
||||
const body = this.constructor.jsonTypes.includes(contentType)
|
||||
? await resp.json()
|
||||
: await resp.text();
|
||||
|
||||
if (!resp.ok) {
|
||||
await this.handleError(resp, body);
|
||||
}
|
||||
return body as R;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected <E> async handleError(resp: Response, body): Promise<undefined, ApiError<E>> {
|
||||
throw {body: body};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user