assets.ts•3.75 kB
/**
* Generated by orval v7.2.0 🍺
* Do not edit manually.
* storyden
* Storyden social API for building community driven platforms.
The Storyden API does not adhere to semantic versioning but instead applies a rolling strategy with deprecations and minimal breaking changes. This has been done mainly for a simpler development process and it may be changed to a more fixed versioning strategy in the future. Ultimately, the primary way Storyden tracks versions is dates, there are no set release tags currently.
* OpenAPI spec version: v1.25.8-canary
*/
import useSwr from "swr";
import type { Key, SWRConfiguration } from "swr";
import useSWRMutation from "swr/mutation";
import type { SWRMutationConfiguration } from "swr/mutation";
import { fetcher } from "../client";
import type {
AssetGetOKResponse,
AssetUploadBody,
AssetUploadOKResponse,
AssetUploadParams,
InternalServerErrorResponse,
NotFoundResponse,
UnauthorisedResponse,
} from "../openapi-schema";
/**
* Upload and process a media file.
*/
export const assetUpload = (
assetUploadBody: AssetUploadBody,
params?: AssetUploadParams,
) => {
return fetcher<AssetUploadOKResponse>({
url: `/assets`,
method: "POST",
headers: { "Content-Type": "application/octet-stream" },
data: assetUploadBody,
params,
});
};
export const getAssetUploadMutationFetcher = (params?: AssetUploadParams) => {
return (
_: Key,
{ arg }: { arg: AssetUploadBody },
): Promise<AssetUploadOKResponse> => {
return assetUpload(arg, params);
};
};
export const getAssetUploadMutationKey = (params?: AssetUploadParams) =>
[`/assets`, ...(params ? [params] : [])] as const;
export type AssetUploadMutationResult = NonNullable<
Awaited<ReturnType<typeof assetUpload>>
>;
export type AssetUploadMutationError =
| UnauthorisedResponse
| InternalServerErrorResponse;
export const useAssetUpload = <
TError = UnauthorisedResponse | InternalServerErrorResponse,
>(
params?: AssetUploadParams,
options?: {
swr?: SWRMutationConfiguration<
Awaited<ReturnType<typeof assetUpload>>,
TError,
Key,
AssetUploadBody,
Awaited<ReturnType<typeof assetUpload>>
> & { swrKey?: string };
},
) => {
const { swr: swrOptions } = options ?? {};
const swrKey = swrOptions?.swrKey ?? getAssetUploadMutationKey(params);
const swrFn = getAssetUploadMutationFetcher(params);
const query = useSWRMutation(swrKey, swrFn, swrOptions);
return {
swrKey,
...query,
};
};
/**
* Download an asset by its ID.
*/
export const assetGet = (assetFilename: string) => {
return fetcher<AssetGetOKResponse>({
url: `/assets/${assetFilename}`,
method: "GET",
});
};
export const getAssetGetKey = (assetFilename: string) =>
[`/assets/${assetFilename}`] as const;
export type AssetGetQueryResult = NonNullable<
Awaited<ReturnType<typeof assetGet>>
>;
export type AssetGetQueryError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse;
export const useAssetGet = <
TError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse,
>(
assetFilename: string,
options?: {
swr?: SWRConfiguration<Awaited<ReturnType<typeof assetGet>>, TError> & {
swrKey?: Key;
enabled?: boolean;
};
},
) => {
const { swr: swrOptions } = options ?? {};
const isEnabled = swrOptions?.enabled !== false && !!assetFilename;
const swrKey =
swrOptions?.swrKey ??
(() => (isEnabled ? getAssetGetKey(assetFilename) : null));
const swrFn = () => assetGet(assetFilename);
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(
swrKey,
swrFn,
swrOptions,
);
return {
swrKey,
...query,
};
};