InvokeAI/invokeai/frontend/web/src/services/api/guards.ts
psychedelicious e386b5dc53 feat(ui): api layer refactor
*migrate from `openapi-typescript-codegen` to `openapi-typescript` and `openapi-fetch`*

`openapi-typescript-codegen` is not very actively maintained - it's been over a year since the last update.
`openapi-typescript` and `openapi-fetch` are part of the actively maintained repo. key differences:

- provides a `fetch` client instead of `axios`, which means we need to be a bit more verbose with typing thunks
- fetch client is created at runtime and has a very nice typescript DX
- generates a single file with all types in it, from which we then extract individual types. i don't like how verbose this is, but i do like how it is more explicit.
- removed npm api generation scripts - now we have a single `typegen` script

overall i have more confidence in this new library.

*use nanostores for api base and token*

very simple reactive store for api base url and token. this was suggested in the `openapi-fetch` docs and i quite like the strategy.

*organise rtk-query api*

split out each endpoint (models, images, boards, boardImages) into their own api extensions. tidy!
2023-06-24 17:57:39 +10:00

78 lines
2.2 KiB
TypeScript

import { get, isObject, isString } from 'lodash-es';
import {
GraphExecutionState,
GraphInvocationOutput,
ImageOutput,
MaskOutput,
PromptOutput,
IterateInvocationOutput,
CollectInvocationOutput,
ImageField,
LatentsOutput,
ResourceOrigin,
ImageDTO,
BoardDTO,
} from 'services/api/types';
export const isImageDTO = (obj: unknown): obj is ImageDTO => {
return (
isObject(obj) &&
'image_name' in obj &&
isString(obj?.image_name) &&
'thumbnail_url' in obj &&
isString(obj?.thumbnail_url) &&
'image_url' in obj &&
isString(obj?.image_url) &&
'image_origin' in obj &&
isString(obj?.image_origin) &&
'created_at' in obj &&
isString(obj?.created_at)
);
};
export const isBoardDTO = (obj: unknown): obj is BoardDTO => {
return (
isObject(obj) &&
'board_id' in obj &&
isString(obj?.board_id) &&
'board_name' in obj &&
isString(obj?.board_name)
);
};
export const isImageOutput = (
output: GraphExecutionState['results'][string]
): output is ImageOutput => output?.type === 'image_output';
export const isLatentsOutput = (
output: GraphExecutionState['results'][string]
): output is LatentsOutput => output?.type === 'latents_output';
export const isMaskOutput = (
output: GraphExecutionState['results'][string]
): output is MaskOutput => output?.type === 'mask';
export const isPromptOutput = (
output: GraphExecutionState['results'][string]
): output is PromptOutput => output?.type === 'prompt';
export const isGraphOutput = (
output: GraphExecutionState['results'][string]
): output is GraphInvocationOutput => output?.type === 'graph_output';
export const isIterateOutput = (
output: GraphExecutionState['results'][string]
): output is IterateInvocationOutput => output?.type === 'iterate_output';
export const isCollectOutput = (
output: GraphExecutionState['results'][string]
): output is CollectInvocationOutput => output?.type === 'collect_output';
export const isResourceOrigin = (t: unknown): t is ResourceOrigin =>
isString(t) && ['internal', 'external'].includes(t);
export const isImageField = (imageField: unknown): imageField is ImageField =>
isObject(imageField) &&
isString(get(imageField, 'image_name')) &&
isResourceOrigin(get(imageField, 'image_origin'));