-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18532 from davelopez/upgrade_openapi_typescript
Upgrade `openapi-typescript` to 7.0.2 + swap to `openapi-fetch`
- Loading branch information
Showing
223 changed files
with
14,538 additions
and
5,405 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { HttpResponse } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
import { createOpenApiHttp } from "openapi-msw"; | ||
|
||
import { type GalaxyApiPaths } from "@/api/schema"; | ||
|
||
export { HttpResponse }; | ||
|
||
function createApiClientMock() { | ||
return createOpenApiHttp<GalaxyApiPaths>({ baseUrl: window.location.origin }); | ||
} | ||
|
||
let http: ReturnType<typeof createApiClientMock>; | ||
let server: ReturnType<typeof setupServer>; | ||
|
||
/** | ||
* Returns a `server` instance that can be used to mock the Galaxy API server | ||
* and make requests to the Galaxy API using the OpenAPI schema. | ||
* | ||
* It is an instance of Mock Service Worker (MSW) server (https://github.com/mswjs/msw). | ||
* And the `http` object is an instance of OpenAPI-MSW (https://github.com/christoph-fricke/openapi-msw) | ||
* that add support for full type inference from OpenAPI schema definitions. | ||
*/ | ||
export function useServerMock() { | ||
if (!server) { | ||
server = setupServer(); | ||
http = createApiClientMock(); | ||
} | ||
|
||
beforeAll(() => { | ||
// Enable API mocking before all the tests. | ||
server.listen({ | ||
onUnhandledRequest: (request) => { | ||
const method = request.method.toLowerCase(); | ||
const apiPath = request.url.replace(window.location.origin, ""); | ||
const errorMessage = ` | ||
No request handler found for ${request.method} ${request.url}. | ||
Make sure you have added a request handler for this request in your tests. | ||
Example: | ||
const { server, http } = useServerMock(); | ||
server.use( | ||
http.${method}('${apiPath}', ({ response }) => { | ||
return response(200).json({}); | ||
}) | ||
); | ||
`; | ||
throw new Error(errorMessage); | ||
}, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
// Reset the request handlers between each test. | ||
// This way the handlers we add on a per-test basis | ||
// do not leak to other, irrelevant tests. | ||
server.resetHandlers(); | ||
}); | ||
|
||
afterAll(() => { | ||
// Finally, disable API mocking after the tests are done. | ||
server.close(); | ||
}); | ||
|
||
return { server, http }; | ||
} |
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,32 @@ | ||
import createClient from "openapi-fetch"; | ||
|
||
import { type GalaxyApiPaths } from "@/api/schema"; | ||
import { getAppRoot } from "@/onload/loadConfig"; | ||
|
||
function getBaseUrl() { | ||
const isTest = process.env.NODE_ENV === "test"; | ||
return isTest ? window.location.origin : getAppRoot(undefined, true); | ||
} | ||
|
||
function apiClientFactory() { | ||
return createClient<GalaxyApiPaths>({ baseUrl: getBaseUrl() }); | ||
} | ||
|
||
export type GalaxyApiClient = ReturnType<typeof apiClientFactory>; | ||
|
||
let client: GalaxyApiClient; | ||
|
||
/** | ||
* Returns the Galaxy API client. | ||
* | ||
* It can be used to make requests to the Galaxy API using the OpenAPI schema. | ||
* | ||
* See: https://openapi-ts.dev/openapi-fetch/ | ||
*/ | ||
export function GalaxyApi(): GalaxyApiClient { | ||
if (!client) { | ||
client = apiClientFactory(); | ||
} | ||
|
||
return client; | ||
} |
Oops, something went wrong.