Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions oxide-api/src/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

/* eslint-disable */

import type { FetchParams } from "./http-client";
import { HttpClient, toQueryString } from "./http-client";

export type {
ApiConfig,
ApiResult,
ErrorBody,
ErrorResult,
import type { FetchParams, FullParams, ApiResult } from "./http-client";
import {
dateReplacer,
handleResponse,
mergeParams,
toQueryString,
} from "./http-client";
import { snakeify } from "./util";

export type { ApiResult, ErrorBody, ErrorResult } from "./http-client";

/**
* An IPv4 subnet
Expand Down Expand Up @@ -6668,7 +6669,47 @@ export interface WebhookSecretsDeletePathParams {
}

type EmptyObj = Record<string, never>;
export class Api extends HttpClient {
export interface ApiConfig {
/**
* No host means requests will be sent to the current host. This is used in
* the web console.
*/
host?: string;
token?: string;
baseParams?: FetchParams;
}

export class Api {
host: string;
token?: string;
baseParams: FetchParams;

constructor({ host = "", baseParams = {}, token }: ApiConfig = {}) {
this.host = host;
this.token = token;

const headers = new Headers({ "Content-Type": "application/json" });
if (token) {
headers.append("Authorization", `Bearer ${token}`);
}
this.baseParams = mergeParams({ headers }, baseParams);
}

public async request<Data>({
body,
path,
query,
host,
...fetchParams
}: FullParams): Promise<ApiResult<Data>> {
const url = (host || this.host) + path + toQueryString(query);
const init = {
...mergeParams(this.baseParams, fetchParams),
body: JSON.stringify(snakeify(body), dateReplacer),
};
return handleResponse(await fetch(url, init));
}

methods = {
/**
* Start an OAuth 2.0 Device Authorization Grant
Expand Down
48 changes: 3 additions & 45 deletions oxide-api/src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Copyright Oxide Computer Company
*/

import { camelToSnake, processResponseBody, snakeify, isNotNull } from "./util";
import { camelToSnake, processResponseBody, isNotNull } from "./util";

/** Success responses from the API */
export type ApiSuccess<Data> = {
Expand Down Expand Up @@ -47,7 +47,7 @@ export type ApiResult<Data> = ApiSuccess<Data> | ErrorResult;
* body and query params.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function replacer(_key: string, value: any) {
export function dateReplacer(_key: string, value: any) {
if (value instanceof Date) {
return value.toISOString();
}
Expand All @@ -56,7 +56,7 @@ function replacer(_key: string, value: any) {

function encodeQueryParam(key: string, value: unknown) {
return `${encodeURIComponent(camelToSnake(key))}=${encodeURIComponent(
replacer(key, value),
dateReplacer(key, value),
)}`;
}

Expand Down Expand Up @@ -117,48 +117,6 @@ export interface FullParams extends FetchParams {
method?: string;
}

export interface ApiConfig {
/**
* No host means requests will be sent to the current host. This is used in
* the web console.
*/
host?: string;
token?: string;
baseParams?: FetchParams;
}

export class HttpClient {
host: string;
token?: string;
baseParams: FetchParams;

constructor({ host = "", baseParams = {}, token }: ApiConfig = {}) {
this.host = host;
this.token = token;

const headers = new Headers({ "Content-Type": "application/json" });
if (token) {
headers.append("Authorization", `Bearer ${token}`);
}
this.baseParams = mergeParams({ headers }, baseParams);
}

public async request<Data>({
body,
path,
query,
host,
...fetchParams
}: FullParams): Promise<ApiResult<Data>> {
const url = (host || this.host) + path + toQueryString(query);
const init = {
...mergeParams(this.baseParams, fetchParams),
body: JSON.stringify(snakeify(body), replacer),
};
return handleResponse(await fetch(url, init));
}
}

export function mergeParams(a: FetchParams, b: FetchParams): FetchParams {
// calling `new Headers()` normalizes `HeadersInit`, which could be a Headers
// object, a plain object, or an array of tuples
Expand Down
52 changes: 47 additions & 5 deletions oxide-openapi-gen-ts/src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ export function generateApi(spec: OpenAPIV3.Document, destDir: string) {

w(`/* eslint-disable */

import type { FetchParams } from './http-client'
import { HttpClient, toQueryString } from './http-client'
import type { FetchParams, FullParams, ApiResult } from "./http-client";
import { dateReplacer, handleResponse, mergeParams, toQueryString } from './http-client'
import { snakeify } from './util'

export type { ApiConfig, ApiResult, ErrorBody, ErrorResult, } from './http-client'
export type { ApiResult, ErrorBody, ErrorResult } from './http-client'
`);

const schemaNames = getSortedSchemas(spec);
Expand Down Expand Up @@ -169,8 +170,49 @@ export function generateApi(spec: OpenAPIV3.Document, destDir: string) {

w("type EmptyObj = Record<string, never>;");

w(`export class Api extends HttpClient {
methods = {`);
w(`export interface ApiConfig {
/**
* No host means requests will be sent to the current host. This is used in
* the web console.
*/
host?: string;
token?: string;
baseParams?: FetchParams;
}

export class Api {
host: string;
token?: string;
baseParams: FetchParams;


constructor({ host = "", baseParams = {}, token }: ApiConfig = {}) {
this.host = host;
this.token = token;

const headers = new Headers({ "Content-Type": "application/json" });
if (token) {
headers.append("Authorization", \`Bearer \${token}\`);
}
this.baseParams = mergeParams({ headers }, baseParams);
}

public async request<Data>({
body,
path,
query,
host,
...fetchParams
}: FullParams): Promise<ApiResult<Data>> {
const url = (host || this.host) + path + toQueryString(query);
const init = {
...mergeParams(this.baseParams, fetchParams),
body: JSON.stringify(snakeify(body), dateReplacer),
};
return handleResponse(await fetch(url, init));
}

methods = {`);

for (const { conf, opId, method, path } of iterPathConfig(spec.paths)) {
// websockets handled in the next loop
Expand Down
48 changes: 3 additions & 45 deletions oxide-openapi-gen-ts/src/client/static/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Copyright Oxide Computer Company
*/

import { camelToSnake, processResponseBody, snakeify, isNotNull } from "./util";
import { camelToSnake, processResponseBody, isNotNull } from "./util";

/** Success responses from the API */
export type ApiSuccess<Data> = {
Expand Down Expand Up @@ -47,7 +47,7 @@ export type ApiResult<Data> = ApiSuccess<Data> | ErrorResult;
* body and query params.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function replacer(_key: string, value: any) {
export function dateReplacer(_key: string, value: any) {
if (value instanceof Date) {
return value.toISOString();
}
Expand All @@ -56,7 +56,7 @@ function replacer(_key: string, value: any) {

function encodeQueryParam(key: string, value: unknown) {
return `${encodeURIComponent(camelToSnake(key))}=${encodeURIComponent(
replacer(key, value)
dateReplacer(key, value)
)}`;
}

Expand Down Expand Up @@ -117,48 +117,6 @@ export interface FullParams extends FetchParams {
method?: string;
}

export interface ApiConfig {
/**
* No host means requests will be sent to the current host. This is used in
* the web console.
*/
host?: string;
token?: string;
baseParams?: FetchParams;
}

export class HttpClient {
host: string;
token?: string;
baseParams: FetchParams;

constructor({ host = "", baseParams = {}, token }: ApiConfig = {}) {
this.host = host;
this.token = token;

const headers = new Headers({ "Content-Type": "application/json" });
if (token) {
headers.append("Authorization", `Bearer ${token}`);
}
this.baseParams = mergeParams({ headers }, baseParams);
}

public async request<Data>({
body,
path,
query,
host,
...fetchParams
}: FullParams): Promise<ApiResult<Data>> {
const url = (host || this.host) + path + toQueryString(query);
const init = {
...mergeParams(this.baseParams, fetchParams),
body: JSON.stringify(snakeify(body), replacer),
};
return handleResponse(await fetch(url, init));
}
}

export function mergeParams(a: FetchParams, b: FetchParams): FetchParams {
// calling `new Headers()` normalizes `HeadersInit`, which could be a Headers
// object, a plain object, or an array of tuples
Expand Down
Loading