Skip to content

Commit

Permalink
feat(api): implement custom api types (#257)
Browse files Browse the repository at this point in the history
* feat: implement custom api types

* fix(types): plural
  • Loading branch information
catouberos authored Dec 8, 2023
1 parent 04d7068 commit 5a313a0
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
16 changes: 16 additions & 0 deletions types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { ListResult } from "pocketbase";
import type { BaseSystemFields } from "@/types/pb";

export type BaseAPIFields = Omit<
BaseSystemFields,
"collectionId" | "collectionName" | "expand"
>;

export type BaseAPIListResult<T> = {
success: boolean;
} & ListResult<T & BaseAPIFields>;

export type BaseAPISingleResult<T> = {
success: boolean;
item: T;
};
100 changes: 100 additions & 0 deletions types/collections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type {
BaseAPIFields,
BaseAPISingleResult,
BaseAPIListResult,
} from "@/types/api";
import type {
CollectionsRecord,
CollectionsResponse as PBCollectionsResponse,
CollectionBooksResponse as PBCollectionBooksResponse,
CollectionMembersResponse as PBCollectionMembersResponse,
UsersRecord,
CollectionMembersRoleOptions,
BooksRecord,
CollectionBooksStatusOptions,
} from "@/types/pb";

/**
* Collection items returned from `/api/user-collections/:userId`
*
* Slightly modified (and similar to) PocketBase {@link PBCollectionsResponse|CollectionsResponse} schema
*
* `collectionId` here is explicitly "Tana.moe user's books collection", not "PocketBase collections"
*
* @see {@link UserCollectionsResponse}
*/
export type CollectionResponse = {
collectionId: string;
userId: string;
collection?: CollectionsRecord & BaseAPIFields;
user?: UsersRecord & BaseAPIFields;
role: CollectionMembersRoleOptions;
};

/**
* User collections returned from `/api/user-collections/:userId`
*
* @see {@link https://github.com/tanamoe/momoka-lite/blob/master/models/collection_member.go}
*/
export type UserCollectionsResponse = BaseAPIListResult<CollectionResponse>;

/**
* User collection returned from `/api/user-collection/:collectionId`
*
* @see {@link https://github.com/tanamoe/momoka-lite/blob/master/models/collection.go}
*/
export type UserCollectionResponse = BaseAPISingleResult<
CollectionsRecord & BaseAPIFields
>;

/**
* Book items returned from `/api/user-collection/:userId/books`
*
* Slightly modified (and similar to) PocketBase {@link PBCollectionBooksResponse|CollectionBooksResponse} schema
*
* `collectionId` here is explicitly "Tana.moe user's books collection", not "PocketBase collections"
*
* @see {@link UserCollectionBooksResponse}
*/
export type CollectionBookResponse = {
collectionId: string;
bookId: string;
collection?: CollectionsRecord & BaseAPIFields;
book?: BooksRecord & BaseAPIFields;
quantity: number;
status: CollectionBooksStatusOptions;
};

/**
* User collection books returned from `/api/user-collection/:userId/books`
*
* @see {@link https://github.com/tanamoe/momoka-lite/blob/master/models/collection_book.go}
*/
export type UserCollectionBooksResponse =
BaseAPIListResult<CollectionBookResponse>;

/**
* Members items returned from `/api/user-collection/:userId/members`
*
* Slightly modified (and similar to) PocketBase {@link PBCollectionMembersResponse|CollectionMembersResponse} schema
*
* `collectionId` here is explicitly "Tana.moe user's books collection", not "PocketBase collections"
*
* @see {@link UserCollectionMembersResponse}
*/
export type CollectionMemberResponse = {
collectionId: string;
userId: string;
collection?: CollectionsRecord & BaseAPIFields;
user?: UsersRecord & BaseAPIFields;
role: CollectionMembersRoleOptions;
};

/**
* User collection members returned from `/api/user-collection/:userId/members`
*
* @see {@link https://github.com/tanamoe/momoka-lite/blob/master/models/collection_member.go}
*/
export type UserCollectionMembersResponse =
BaseAPIListResult<CollectionMemberResponse>;

0 comments on commit 5a313a0

Please sign in to comment.