ft_transcendence/openapi-template/api_example.mustache
Maieul BOYER 5dd6067c95 feat(openapi): Add modified typescript-fetch template
This template is modified from the original one, to handle multiple
status-code handling of response.

Don't ask me how they work, I don't quite understand them in depth...
2025-11-10 18:34:22 +01:00

44 lines
1.6 KiB
Text

import {
Configuration,
{{classname}},
} from '{{npmName}}';
import type { {{operationIdCamelCase}}Request } from '{{npmName}}';
async function example() {
console.log("🚀 Testing {{npmName}} SDK...");
{{#hasAuthMethods}}
const config = new Configuration({ {{#authMethods}}{{#isBasicBasic}}
// To configure HTTP basic authorization: {{{name}}}
username: "YOUR USERNAME",
password: "YOUR PASSWORD",{{/isBasicBasic}}{{#isBasicBearer}}
// Configure HTTP bearer authorization: {{{name}}}
accessToken: "YOUR BEARER TOKEN",{{/isBasicBearer}}{{#isOAuth}}
// To configure OAuth2 access token for authorization: {{{name}}} {{{flow}}}
accessToken: "YOUR ACCESS TOKEN",{{/isOAuth}}{{#isApiKey}}
// To configure API key authorization: {{{name}}}
apiKey: "YOUR API KEY",{{/isApiKey}}{{#isHttpSignature}}
// To configure HTTP signature authorization: {{{name}}}
headers: { "YOUR HEADER NAME": "YOUR SIGNATURE" },{{/isHttpSignature}}{{/authMethods}}
});
{{/hasAuthMethods}}
const api = new {{classname}}({{#hasAuthMethods}}config{{/hasAuthMethods}});
{{#hasParams}}
const body = {
{{#allParams}}
// {{{dataType}}}{{#description}} | {{{description}}}{{/description}}{{^required}} (optional){{/required}}
{{paramName}}: {{{example}}}{{^example}}...{{/example}},
{{/allParams}}
} satisfies {{operationIdCamelCase}}Request;
{{/hasParams}}
try {
const data = await api.{{{operationId}}}({{#hasParams}}body{{/hasParams}});
console.log(data);
} catch (error) {
console.error(error);
}
}
// Run the test
example().catch(console.error);