Files
upstream-corteza/lib/js/tools/codegen/template.js
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

157 lines
3.5 KiB
JavaScript

export const template = `
/* eslint-disable padded-blocks */
// This is a generated file.
// See README.md file for update instructions
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
interface KV {
[header: string]: unknown;
}
interface Headers {
[header: string]: string;
}
interface Ctor {
baseURL?: string;
accessTokenFn?: () => string | undefined;
headers?: Headers;
}
interface CortezaResponse {
error?: string;
response?: unknown;
}
interface ExtraConfig {
headers?: Headers;
}
function stdResolve (response: AxiosResponse<CortezaResponse>): KV|Promise<never> {
if (response.data.error) {
return Promise.reject(response.data.error)
} else {
return response.data.response as KV
}
}
export default class {{className}} {
protected baseURL?: string;
protected accessTokenFn?: () => (string | undefined);
protected headers: Headers = {};
constructor ({ baseURL, headers, accessTokenFn }: Ctor) {
this.baseURL = baseURL
this.accessTokenFn = accessTokenFn
this.headers = {
/**
* All we send is JSON
*/
'Content-Type': 'application/json',
}
this.setHeaders(headers)
}
setAccessTokenFn (fn: () => string | undefined): {{className}} {
this.accessTokenFn = fn
return this
}
setHeaders (headers?: Headers): {{className}} {
if (typeof headers === 'object') {
this.headers = headers
}
return this
}
setHeader (name: string, value: string | undefined): {{className}} {
if (value === undefined) {
delete this.headers[name]
} else {
this.headers[name] = value
}
return this
}
api (): AxiosInstance {
const headers = { ...this.headers }
const accessToken = this.accessTokenFn ? this.accessTokenFn() : undefined
if (accessToken) {
headers.Authorization = 'Bearer ' + accessToken
}
return axios.create({
withCredentials: true,
baseURL: this.baseURL,
headers,
})
}
{{#endpoints}}
// {{title}}{{#description}}
// {{description}}{{/description}}
async {{fname}} ({{#if fargs}}a: KV, {{/if}}extra: AxiosRequestConfig = {}): Promise<KV> {
{{#if fargs}}const {
{{#fargs}}
{{.}},
{{/fargs}}
} = (a as KV) || {}{{/if}}
{{#required}}
if (!{{.}}) {
throw Error('field {{.}} is empty')
}
{{/required}}
const cfg: AxiosRequestConfig = {
...extra,
method: '{{method}}',
url: this.{{fname}}Endpoint({{#if pathParams}}{
{{#pathParams}}{{.}}, {{/pathParams}}
}{{/if}}),
}
{{#hasParams}}cfg.params = {
{{#params}}
{{.}},
{{/params}}
}
{{/hasParams}}{{#hasData}}cfg.data = {
{{#data}}
{{.}},
{{/data}}
}{{/hasData}}
return this.api().request(cfg).then(result => stdResolve(result))
}
{{fname}}Cancellable ({{#if fargs}}a: KV, {{/if}}extra: AxiosRequestConfig = {}): { response: (a: KV, extra?: AxiosRequestConfig) => Promise<KV>; cancel: () => void; } {
const cancelTokenSource = axios.CancelToken.source();
const options = {...extra, cancelToken: cancelTokenSource.token }
return {
response: () => this.{{fname}}({{#if fargs}}a, {{/if}}options),
cancel: () => {
cancelTokenSource.cancel();
},
}
}
{{fname}}Endpoint ({{#if pathParams}}a: KV{{/if}}): string {
{{#if pathParams}}
const {
{{#pathParams}}
{{.}},
{{/pathParams}}
} = a || {}
return \`{{path}}\`
{{else}}
return '{{path}}'
{{/if}}
}
{{/endpoints}}
}
`