Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👽️(frontend) Generate typescript api client - winter 2023 #2142

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions src/frontend/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ i18n/**/*
public-path.js
# LexPersona iframe script
iframe-manager.js
# generated api client
js/api/**/gen/**/*.ts
39 changes: 39 additions & 0 deletions src/frontend/js/api/joanie/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fetchMock from 'fetch-mock';
import { RICHIE_USER_TOKEN } from 'settings';
import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie';
import { getApiClientJoanie } from './client';
import { ApiClientJoanie } from './gen';

jest.mock('utils/context', () => ({
__esModule: true,
default: mockRichieContextFactory({
authentication: { backend: 'fonzie', endpoint: 'https://demo.endpoint' },
joanie_backend: { endpoint: 'https://joanie.endpoint' },
}).one(),
}));

describe('joanieApi', () => {
let joanieApi: ApiClientJoanie;
beforeEach(() => {
joanieApi = getApiClientJoanie();
});

it('test', async () => {
fetchMock.get('https://joanie.endpoint/api/v1.0/addresses/FAKE_ADDRESS_ID/', []);
await joanieApi.addresses.addressesRetrieve('FAKE_ADDRESS_ID');

let lastCall = fetchMock.lastCall();
const visitorHeader = lastCall && lastCall[1]?.headers;
// TS see visitorHeader has HeadersInit instead of Headers and
// didn't accept get() as a possible function.
// @ts-ignore
expect(visitorHeader?.get('Authorization')).toBeNull();

sessionStorage.setItem(RICHIE_USER_TOKEN, 'TEST_TOKEN');
await joanieApi.addresses.addressesRetrieve('FAKE_ADDRESS_ID');
lastCall = fetchMock.lastCall();
const userHeader = lastCall && lastCall[1]?.headers;
// @ts-ignore
expect(userHeader?.get('Authorization')).toBe('Bearer TEST_TOKEN');
});
});
35 changes: 35 additions & 0 deletions src/frontend/js/api/joanie/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { RICHIE_USER_TOKEN } from 'settings';

import context from 'utils/context';
import { ApiClientJoanie, ApiError, OpenAPIConfig } from './gen';

/**
* Build Joanie API Routes interface.
*/
export const getAPIEndpoint = () => {
const endpoint = context?.joanie_backend?.endpoint;

if (!endpoint) {
throw new Error('[JOANIE] - Joanie API endpoint is not defined.');
}

return `${endpoint}`;
};

export const isApiError = (error: unknown): error is ApiError => {
return (error as ApiError).name === 'ApiError';
};

export const getApiClientJoanie = () => {
const config: OpenAPIConfig = {
BASE: getAPIEndpoint(),
VERSION: '1',
WITH_CREDENTIALS: false,
CREDENTIALS: 'omit',
TOKEN: async () => {
return sessionStorage.getItem(RICHIE_USER_TOKEN) || '';
},
};

return new ApiClientJoanie(config);
};
77 changes: 77 additions & 0 deletions src/frontend/js/api/joanie/gen/ApiClientJoanie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { BaseHttpRequest } from './core/BaseHttpRequest';
import type { OpenAPIConfig } from './core/OpenAPI';
import { FetchHttpRequest } from './core/FetchHttpRequest';

import { AddressesService } from './services/AddressesService';
import { CertificatesService } from './services/CertificatesService';
import { ContractDefinitionsService } from './services/ContractDefinitionsService';
import { ContractsService } from './services/ContractsService';
import { CourseProductRelationsService } from './services/CourseProductRelationsService';
import { CourseRunsService } from './services/CourseRunsService';
import { CourseRunsSyncService } from './services/CourseRunsSyncService';
import { CoursesService } from './services/CoursesService';
import { CreditCardsService } from './services/CreditCardsService';
import { EnrollmentsService } from './services/EnrollmentsService';
import { OrdersService } from './services/OrdersService';
import { OrganizationsService } from './services/OrganizationsService';
import { PaymentsService } from './services/PaymentsService';
import { SignatureService } from './services/SignatureService';
import { UsersService } from './services/UsersService';

type HttpRequestConstructor = new (config: OpenAPIConfig) => BaseHttpRequest;

export class ApiClientJoanie {

public readonly addresses: AddressesService;
public readonly certificates: CertificatesService;
public readonly contractDefinitions: ContractDefinitionsService;
public readonly contracts: ContractsService;
public readonly courseProductRelations: CourseProductRelationsService;
public readonly courseRuns: CourseRunsService;
public readonly courseRunsSync: CourseRunsSyncService;
public readonly courses: CoursesService;
public readonly creditCards: CreditCardsService;
public readonly enrollments: EnrollmentsService;
public readonly orders: OrdersService;
public readonly organizations: OrganizationsService;
public readonly payments: PaymentsService;
public readonly signature: SignatureService;
public readonly users: UsersService;

public readonly request: BaseHttpRequest;

constructor(config?: Partial<OpenAPIConfig>, HttpRequest: HttpRequestConstructor = FetchHttpRequest) {
this.request = new HttpRequest({
BASE: config?.BASE ?? '',
VERSION: config?.VERSION ?? '1.0.0 (v1.0)',
WITH_CREDENTIALS: config?.WITH_CREDENTIALS ?? false,
CREDENTIALS: config?.CREDENTIALS ?? 'include',
TOKEN: config?.TOKEN,
USERNAME: config?.USERNAME,
PASSWORD: config?.PASSWORD,
HEADERS: config?.HEADERS,
ENCODE_PATH: config?.ENCODE_PATH,
});

this.addresses = new AddressesService(this.request);
this.certificates = new CertificatesService(this.request);
this.contractDefinitions = new ContractDefinitionsService(this.request);
this.contracts = new ContractsService(this.request);
this.courseProductRelations = new CourseProductRelationsService(this.request);
this.courseRuns = new CourseRunsService(this.request);
this.courseRunsSync = new CourseRunsSyncService(this.request);
this.courses = new CoursesService(this.request);
this.creditCards = new CreditCardsService(this.request);
this.enrollments = new EnrollmentsService(this.request);
this.orders = new OrdersService(this.request);
this.organizations = new OrganizationsService(this.request);
this.payments = new PaymentsService(this.request);
this.signature = new SignatureService(this.request);
this.users = new UsersService(this.request);
}
}

25 changes: 25 additions & 0 deletions src/frontend/js/api/joanie/gen/core/ApiError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { ApiRequestOptions } from './ApiRequestOptions';
import type { ApiResult } from './ApiResult';

export class ApiError extends Error {
public readonly url: string;
public readonly status: number;
public readonly statusText: string;
public readonly body: any;
public readonly request: ApiRequestOptions;

constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
super(message);

this.name = 'ApiError';
this.url = response.url;
this.status = response.status;
this.statusText = response.statusText;
this.body = response.body;
this.request = request;
}
}
17 changes: 17 additions & 0 deletions src/frontend/js/api/joanie/gen/core/ApiRequestOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export type ApiRequestOptions = {
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
readonly url: string;
readonly path?: Record<string, any>;
readonly cookies?: Record<string, any>;
readonly headers?: Record<string, any>;
readonly query?: Record<string, any>;
readonly formData?: Record<string, any>;
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly errors?: Record<number, string>;
};
11 changes: 11 additions & 0 deletions src/frontend/js/api/joanie/gen/core/ApiResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export type ApiResult = {
readonly url: string;
readonly ok: boolean;
readonly status: number;
readonly statusText: string;
readonly body: any;
};
14 changes: 14 additions & 0 deletions src/frontend/js/api/joanie/gen/core/BaseHttpRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { ApiRequestOptions } from './ApiRequestOptions';
import type { CancelablePromise } from './CancelablePromise';
import type { OpenAPIConfig } from './OpenAPI';

export abstract class BaseHttpRequest {

constructor(public readonly config: OpenAPIConfig) {}

public abstract request<T>(options: ApiRequestOptions): CancelablePromise<T>;
}
131 changes: 131 additions & 0 deletions src/frontend/js/api/joanie/gen/core/CancelablePromise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export class CancelError extends Error {

constructor(message: string) {
super(message);
this.name = 'CancelError';
}

public get isCancelled(): boolean {
return true;
}
}

export interface OnCancel {
readonly isResolved: boolean;
readonly isRejected: boolean;
readonly isCancelled: boolean;

(cancelHandler: () => void): void;
}

export class CancelablePromise<T> implements Promise<T> {
#isResolved: boolean;
#isRejected: boolean;
#isCancelled: boolean;
readonly #cancelHandlers: (() => void)[];
readonly #promise: Promise<T>;
#resolve?: (value: T | PromiseLike<T>) => void;
#reject?: (reason?: any) => void;

constructor(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void,
onCancel: OnCancel
) => void
) {
this.#isResolved = false;
this.#isRejected = false;
this.#isCancelled = false;
this.#cancelHandlers = [];
this.#promise = new Promise<T>((resolve, reject) => {
this.#resolve = resolve;
this.#reject = reject;

const onResolve = (value: T | PromiseLike<T>): void => {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this.#isResolved = true;
this.#resolve?.(value);
};

const onReject = (reason?: any): void => {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this.#isRejected = true;
this.#reject?.(reason);
};

const onCancel = (cancelHandler: () => void): void => {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this.#cancelHandlers.push(cancelHandler);
};

Object.defineProperty(onCancel, 'isResolved', {
get: (): boolean => this.#isResolved,
});

Object.defineProperty(onCancel, 'isRejected', {
get: (): boolean => this.#isRejected,
});

Object.defineProperty(onCancel, 'isCancelled', {
get: (): boolean => this.#isCancelled,
});

return executor(onResolve, onReject, onCancel as OnCancel);
});
}

get [Symbol.toStringTag]() {
return "Cancellable Promise";
}

public then<TResult1 = T, TResult2 = never>(
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this.#promise.then(onFulfilled, onRejected);
}

public catch<TResult = never>(
onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null
): Promise<T | TResult> {
return this.#promise.catch(onRejected);
}

public finally(onFinally?: (() => void) | null): Promise<T> {
return this.#promise.finally(onFinally);
}

public cancel(): void {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this.#isCancelled = true;
if (this.#cancelHandlers.length) {
try {
for (const cancelHandler of this.#cancelHandlers) {
cancelHandler();
}
} catch (error) {
console.warn('Cancellation threw an error', error);
return;
}
}
this.#cancelHandlers.length = 0;
this.#reject?.(new CancelError('Request aborted'));
}

public get isCancelled(): boolean {
return this.#isCancelled;
}
}
26 changes: 26 additions & 0 deletions src/frontend/js/api/joanie/gen/core/FetchHttpRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { ApiRequestOptions } from './ApiRequestOptions';
import { BaseHttpRequest } from './BaseHttpRequest';
import type { CancelablePromise } from './CancelablePromise';
import type { OpenAPIConfig } from './OpenAPI';
import { request as __request } from './request';

export class FetchHttpRequest extends BaseHttpRequest {

constructor(config: OpenAPIConfig) {
super(config);
}

/**
* Request method
* @param options The request options from the service
* @returns CancelablePromise<T>
* @throws ApiError
*/
public override request<T>(options: ApiRequestOptions): CancelablePromise<T> {
return __request(this.config, options);
}
}
Loading