Skip to content

Instantly share code, notes, and snippets.

@undrcrxwn
Created October 18, 2023 12:46
Show Gist options
  • Save undrcrxwn/221a6f9aae8153f209b26f002b8114de to your computer and use it in GitHub Desktop.
Save undrcrxwn/221a6f9aae8153f209b26f002b8114de to your computer and use it in GitHub Desktop.
// Crowd Parlay API 1.0
// ---
// This file is automatically generated by openapi with preset openapi-preset-effector
// Do not edit this file directly. Instead open openapi config file and follow the link in "file"
import { createEffect } from 'effector-root';
import * as typed from 'typed-contracts';
import { fetchFx } from '../lib/fetch';
//#region prebuilt code
const custom = { any: (valueName: string, value: unknown): any => value }
export type GenericErrors =
| {
status: 'unexpected';
error: Error;
}
| {
status: 'unknown_status';
error: { status: number; body: unknown };
}
| {
status: 'validation_error';
error: typed.ValidationError;
};
type ErrorCodes = 400 | 401 | 402 | 403 | 404 | 405 | 406 | 500 | 501 | 502 | 503 | 503 | 505;
/**
* @throws
*/
function parseByStatus<
Variants extends string,
Contracts extends Record<number, [Variants, typed.Contract<any>]>,
Result extends {
[Code in keyof Contracts]: Contracts[Code] extends [infer Status, typed.Contract<infer T>]
? { status: Status; answer: T }
: never;
}
>(
name: string,
response: { status: number; body?: unknown },
contracts: Contracts,
): Result[Exclude<keyof Result, ErrorCodes>] {
const contractObject = contracts[response.status];
if (!contractObject) {
throw {
status: 'unknown_status',
error: {
status: response.status,
body: response.body,
},
};
}
const [status, contract] = contractObject;
const answer = contract(name, response.body);
if (answer instanceof typed.ValidationError) {
throw { status: 'validation_error', error: answer };
}
if (response.status >= 400) {
throw { status, error: answer };
}
return { status, answer } as Result[Exclude<keyof Result, ErrorCodes>];
}
//#endregion prebuilt code/* --- */
//#region apiV1UsersRegisterPost
export type ApiV1UsersRegisterPost = {
body?: {
username?: string;
display_name?: string;
password?: string;
avatar_url?: string | null;
};
};
/* Success */
export const apiV1UsersRegisterPostOk = typed.object({
id: typed.string.optional,
username: typed.string.optional,
display_name: typed.string.optional,
avatar_url: typed.string.maybe
});
export type ApiV1UsersRegisterPostDone = {
status: "ok";
answer: typed.Get<typeof apiV1UsersRegisterPostOk>;
};
export type ApiV1UsersRegisterPostFail = GenericErrors;
export const apiV1UsersRegisterPost = createEffect<ApiV1UsersRegisterPost, ApiV1UsersRegisterPostDone, ApiV1UsersRegisterPostFail>({
async handler({
body
}) {
const name = "apiV1UsersRegisterPost.body";
const response = await fetchFx({
path: "/api/v1/users/register",
method: "POST",
body
});
return parseByStatus(name, response, {
200: ["ok", apiV1UsersRegisterPostOk]
});
}
});
//#endregion apiV1UsersRegisterPost
/* --- */
//#region apiV1UsersUserIdGet
export type ApiV1UsersUserIdGet = {
path: {
userId: string;
};
};
/* Success */
export const apiV1UsersUserIdGetOk = typed.object({
id: typed.string.optional,
username: typed.string.optional,
display_name: typed.string.optional,
avatar_url: typed.string.maybe
});
export type ApiV1UsersUserIdGetDone = {
status: "ok";
answer: typed.Get<typeof apiV1UsersUserIdGetOk>;
};
export type ApiV1UsersUserIdGetFail = GenericErrors;
export const apiV1UsersUserIdGet = createEffect<ApiV1UsersUserIdGet, ApiV1UsersUserIdGetDone, ApiV1UsersUserIdGetFail>({
async handler({
path
}) {
const name = "apiV1UsersUserIdGet.body";
const response = await fetchFx({
path: `/api/v1/users/${path.userId}`,
method: "GET"
});
return parseByStatus(name, response, {
200: ["ok", apiV1UsersUserIdGetOk]
});
}
});
//#endregion apiV1UsersUserIdGet
/* --- */
//#region apiV1UsersUserIdPut
export type ApiV1UsersUserIdPut = {
body?: {
username?: string | null;
display_name?: string | null;
avatar_url?: string | null;
old_password?: string | null;
new_password?: string | null;
};
path: {
userId: string;
};
};
/* Success */
export const apiV1UsersUserIdPutOk = typed.object({
id: typed.string.optional,
username: typed.string.optional,
display_name: typed.string.optional,
avatar_url: typed.string.maybe
});
export type ApiV1UsersUserIdPutDone = {
status: "ok";
answer: typed.Get<typeof apiV1UsersUserIdPutOk>;
};
export type ApiV1UsersUserIdPutFail = GenericErrors;
export const apiV1UsersUserIdPut = createEffect<ApiV1UsersUserIdPut, ApiV1UsersUserIdPutDone, ApiV1UsersUserIdPutFail>({
async handler({
body,
path
}) {
const name = "apiV1UsersUserIdPut.body";
const response = await fetchFx({
path: `/api/v1/users/${path.userId}`,
method: "PUT",
body
});
return parseByStatus(name, response, {
200: ["ok", apiV1UsersUserIdPutOk]
});
}
});
//#endregion apiV1UsersUserIdPut
/* --- */
//#region apiV1UsersUserIdDelete
export type ApiV1UsersUserIdDelete = {
path: {
userId: string;
};
};
/* Success */
export const apiV1UsersUserIdDeleteOk = typed.nul;
export type ApiV1UsersUserIdDeleteDone = {
status: "ok";
answer: typed.Get<typeof apiV1UsersUserIdDeleteOk>;
};
export type ApiV1UsersUserIdDeleteFail = GenericErrors;
export const apiV1UsersUserIdDelete = createEffect<ApiV1UsersUserIdDelete, ApiV1UsersUserIdDeleteDone, ApiV1UsersUserIdDeleteFail>({
async handler({
path
}) {
const name = "apiV1UsersUserIdDelete.body";
const response = await fetchFx({
path: `/api/v1/users/${path.userId}`,
method: "DELETE"
});
return parseByStatus(name, response, {
200: ["ok", apiV1UsersUserIdDeleteOk]
});
}
});
//#endregion apiV1UsersUserIdDelete
/* --- */
//#region apiV1UsersResolveGet
export type ApiV1UsersResolveGet = {
query?: {
username?: string;
};
};
/* Success */
export const apiV1UsersResolveGetOk = typed.object({
id: typed.string.optional,
username: typed.string.optional,
display_name: typed.string.optional,
avatar_url: typed.string.maybe
});
export type ApiV1UsersResolveGetDone = {
status: "ok";
answer: typed.Get<typeof apiV1UsersResolveGetOk>;
};
export type ApiV1UsersResolveGetFail = GenericErrors;
export const apiV1UsersResolveGet = createEffect<ApiV1UsersResolveGet, ApiV1UsersResolveGetDone, ApiV1UsersResolveGetFail>({
async handler({
query
}) {
const name = "apiV1UsersResolveGet.body";
const response = await fetchFx({
path: "/api/v1/users/resolve",
method: "GET",
query
});
return parseByStatus(name, response, {
200: ["ok", apiV1UsersResolveGetOk]
});
}
});
//#endregion apiV1UsersResolveGet
/* --- */
//#region apiV1AuthorsAuthorIdGet
export type ApiV1AuthorsAuthorIdGet = {
path: {
authorId: string;
};
};
/* Success */
export const apiV1AuthorsAuthorIdGetOk = typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
});
export type ApiV1AuthorsAuthorIdGetDone = {
status: "ok";
answer: typed.Get<typeof apiV1AuthorsAuthorIdGetOk>;
};
export type ApiV1AuthorsAuthorIdGetFail = GenericErrors;
export const apiV1AuthorsAuthorIdGet = createEffect<ApiV1AuthorsAuthorIdGet, ApiV1AuthorsAuthorIdGetDone, ApiV1AuthorsAuthorIdGetFail>({
async handler({
path
}) {
const name = "apiV1AuthorsAuthorIdGet.body";
const response = await fetchFx({
path: `/api/v1/authors/${path.authorId}`,
method: "GET"
});
return parseByStatus(name, response, {
200: ["ok", apiV1AuthorsAuthorIdGetOk]
});
}
});
//#endregion apiV1AuthorsAuthorIdGet
/* --- */
//#region apiV1CommentsCommentIdGet
export type ApiV1CommentsCommentIdGet = {
path: {
commentId: string;
};
};
/* Success */
export const apiV1CommentsCommentIdGetOk = typed.object({
id: typed.string.optional,
content: typed.string.optional,
author: typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
}).optional,
createdAt: typed.string.optional,
replyCount: typed.number.optional,
firstRepliesAuthors: typed.array(typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
})).optional
});
export type ApiV1CommentsCommentIdGetDone = {
status: "ok";
answer: typed.Get<typeof apiV1CommentsCommentIdGetOk>;
};
export type ApiV1CommentsCommentIdGetFail = GenericErrors;
export const apiV1CommentsCommentIdGet = createEffect<ApiV1CommentsCommentIdGet, ApiV1CommentsCommentIdGetDone, ApiV1CommentsCommentIdGetFail>({
async handler({
path
}) {
const name = "apiV1CommentsCommentIdGet.body";
const response = await fetchFx({
path: `/api/v1/comments/${path.commentId}`,
method: "GET"
});
return parseByStatus(name, response, {
200: ["ok", apiV1CommentsCommentIdGetOk]
});
}
});
//#endregion apiV1CommentsCommentIdGet
/* --- */
//#region apiV1CommentsGet
export type ApiV1CommentsGet = {
query?: {
authorId?: string;
};
};
/* Success */
export const apiV1CommentsGetOk = typed.array(typed.object({
id: typed.string.optional,
content: typed.string.optional,
author: typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
}).optional,
createdAt: typed.string.optional,
replyCount: typed.number.optional,
firstRepliesAuthors: typed.array(typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
})).optional
}));
export type ApiV1CommentsGetDone = {
status: "ok";
answer: typed.Get<typeof apiV1CommentsGetOk>;
};
export type ApiV1CommentsGetFail = GenericErrors;
export const apiV1CommentsGet = createEffect<ApiV1CommentsGet, ApiV1CommentsGetDone, ApiV1CommentsGetFail>({
async handler({
query
}) {
const name = "apiV1CommentsGet.body";
const response = await fetchFx({
path: "/api/v1/comments",
method: "GET",
query
});
return parseByStatus(name, response, {
200: ["ok", apiV1CommentsGetOk]
});
}
});
//#endregion apiV1CommentsGet
/* --- */
//#region apiV1CommentsPost
export type ApiV1CommentsPost = {
body?: {
content?: string;
};
};
/* Success */
export const apiV1CommentsPostOk = typed.object({
id: typed.string.optional,
content: typed.string.optional,
author: typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
}).optional,
createdAt: typed.string.optional,
replyCount: typed.number.optional,
firstRepliesAuthors: typed.array(typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
})).optional
});
export type ApiV1CommentsPostDone = {
status: "ok";
answer: typed.Get<typeof apiV1CommentsPostOk>;
};
export type ApiV1CommentsPostFail = GenericErrors;
export const apiV1CommentsPost = createEffect<ApiV1CommentsPost, ApiV1CommentsPostDone, ApiV1CommentsPostFail>({
async handler({
body
}) {
const name = "apiV1CommentsPost.body";
const response = await fetchFx({
path: "/api/v1/comments",
method: "POST",
body
});
return parseByStatus(name, response, {
200: ["ok", apiV1CommentsPostOk]
});
}
});
//#endregion apiV1CommentsPost
/* --- */
//#region apiV1CommentsTargetCommentIdReplyPost
export type ApiV1CommentsTargetCommentIdReplyPost = {
body?: {
content?: string;
};
path: {
targetCommentId: string;
};
};
/* Success */
export const apiV1CommentsTargetCommentIdReplyPostOk = typed.object({
id: typed.string.optional,
content: typed.string.optional,
author: typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
}).optional,
createdAt: typed.string.optional,
replyCount: typed.number.optional,
firstRepliesAuthors: typed.array(typed.object({
id: typed.string.optional,
username: typed.string.optional,
displayName: typed.string.optional,
avatarUrl: typed.string.maybe
})).optional
});
export type ApiV1CommentsTargetCommentIdReplyPostDone = {
status: "ok";
answer: typed.Get<typeof apiV1CommentsTargetCommentIdReplyPostOk>;
};
export type ApiV1CommentsTargetCommentIdReplyPostFail = GenericErrors;
export const apiV1CommentsTargetCommentIdReplyPost = createEffect<ApiV1CommentsTargetCommentIdReplyPost, ApiV1CommentsTargetCommentIdReplyPostDone, ApiV1CommentsTargetCommentIdReplyPostFail>({
async handler({
body,
path
}) {
const name = "apiV1CommentsTargetCommentIdReplyPost.body";
const response = await fetchFx({
path: `/api/v1/comments/${path.targetCommentId}/reply`,
method: "POST",
body
});
return parseByStatus(name, response, {
200: ["ok", apiV1CommentsTargetCommentIdReplyPostOk]
});
}
});
//#endregion apiV1CommentsTargetCommentIdReplyPost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment