Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom API client generator (#618)
* generate client with new generator * passify tests and types * actually it's npm install in oxide.ts/generator * update omicron to bring in full snakeification * automatically update packer-id * regen with latest generator version * serialize mock api responses as snake case (ugh doesn't work) * upgrade msw for bugfix: port was showing up in request params mswjs/msw#1036 * fix all problems in universe * just use never as empty response body type * use API JSON types, which are cool except they clutter up the generated client * do the same thing with a Json helper instead of generated *JSON types * get Snakify from type-fest. why not * tests for Json type Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f4a704a
commit 292fd0a
Showing
28 changed files
with
894 additions
and
1,764 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import type { Disk, DiskResultsPage } from '@oxide/api' | ||
import type { Json } from './json-type' | ||
import { project } from './project' | ||
|
||
export const disk: Disk = { | ||
export const disk: Json<Disk> = { | ||
id: 'disk-id', | ||
name: 'disk-name', | ||
description: "it's a disk", | ||
projectId: project.id, | ||
timeCreated: new Date().toISOString(), | ||
timeModified: new Date().toISOString(), | ||
project_id: project.id, | ||
time_created: new Date().toISOString(), | ||
time_modified: new Date().toISOString(), | ||
state: { state: 'detached' }, | ||
devicePath: '/uh', | ||
device_path: '/uh', | ||
size: 1000, | ||
} | ||
|
||
export const disks: DiskResultsPage = { items: [disk] } | ||
export const disks: Json<DiskResultsPage> = { items: [disk] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import type { Instance, InstanceResultsPage } from '@oxide/api' | ||
import type { Json } from './json-type' | ||
import { project } from './project' | ||
|
||
export const instance: Instance = { | ||
export const instance: Json<Instance> = { | ||
ncpus: 7, | ||
memory: 1024 * 1024 * 256, | ||
name: 'db1', | ||
description: 'an instance', | ||
id: 'abc123', | ||
hostname: 'oxide.com', | ||
projectId: project.id, | ||
runState: 'running', | ||
timeCreated: new Date().toISOString(), | ||
timeModified: new Date().toISOString(), | ||
timeRunStateUpdated: new Date().toISOString(), | ||
project_id: project.id, | ||
run_state: 'running', | ||
time_created: new Date().toISOString(), | ||
time_modified: new Date().toISOString(), | ||
time_run_state_updated: new Date().toISOString(), | ||
} | ||
|
||
export const instances: InstanceResultsPage = { items: [instance] } | ||
export const instances: Json<InstanceResultsPage> = { items: [instance] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { SnakeCasedPropertiesDeep as Snakify, Simplify } from 'type-fest' | ||
|
||
// these are used for turning our nice JS-ified API types back into the original | ||
// API JSON types (snake cased and dates as strings) for use in our mock API | ||
|
||
type StringifyDates<T> = T extends Date | ||
? string | ||
: { | ||
[K in keyof T]: T[K] extends Array<infer U> | ||
? Array<StringifyDates<U>> | ||
: StringifyDates<T[K]> | ||
} | ||
|
||
/** | ||
* Snake case fields and convert dates to strings. Not intended to be a general | ||
* purpose JSON type! | ||
*/ | ||
// Simplify dramatically improves the IDE type hint. | ||
export type Json<B> = Simplify<Snakify<StringifyDates<B>>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { VpcSubnet } from '@oxide/api' | ||
import type { Json } from './json-type' | ||
|
||
// Tests of a sort. These expectType calls will fail to typecheck if the types | ||
// are not equal. There's no point in wrapping this in a real test because it | ||
// will always pass. | ||
|
||
const expectType = <T>(_value: T) => {} | ||
|
||
let val: any // eslint-disable-line @typescript-eslint/no-explicit-any | ||
|
||
// just checking :) | ||
expectType<1>(val as 1) | ||
// @ts-expect-error | ||
expectType<1>(val as 2) | ||
// @ts-expect-error | ||
expectType<{ x: string }>(val as { x: number }) | ||
|
||
expectType<string>(val as Json<Date>) | ||
expectType<number>(val as Json<number>) | ||
expectType<{ x: string; y: number }>(val as Json<{ x: Date; y: number }>) | ||
expectType<{ x: { a_b45_c: string }; z: string[] }>( | ||
val as Json<{ x: { aB45C: Date }; z: Date[] }> | ||
) | ||
|
||
type VpcSubnetJSON = { | ||
description: string | ||
id: string | ||
ipv4_block?: string | null | ||
ipv6_block?: string | null | ||
name: string | ||
time_created: string | ||
time_modified: string | ||
vpc_id: string | ||
} | ||
|
||
expectType<VpcSubnetJSON>(val as Json<VpcSubnet>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
292fd0a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: