diff options
| author | Mohamed Bassem <me@mbassem.com> | 2024-12-27 19:21:43 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2024-12-27 19:34:04 +0000 |
| commit | 3f56389f55ef116f8cea17c15c684798fb942290 (patch) | |
| tree | 1a398accbcfc75674c5cc4970bc85fd27c637911 /packages/open-api | |
| parent | 86d74e3f32dd5bccc8df195b55391e206df9a1c4 (diff) | |
| download | karakeep-3f56389f55ef116f8cea17c15c684798fb942290.tar.zst | |
feat: Add REST APIs for manipulating highlights. Fixes #620
Diffstat (limited to 'packages/open-api')
| -rw-r--r-- | packages/open-api/hoarder-openapi-spec.json | 93 | ||||
| -rw-r--r-- | packages/open-api/lib/bookmarks.ts | 23 | ||||
| -rw-r--r-- | packages/open-api/lib/highlights.ts | 162 |
3 files changed, 278 insertions, 0 deletions
diff --git a/packages/open-api/hoarder-openapi-spec.json b/packages/open-api/hoarder-openapi-spec.json index a984fcc7..26f0a4b7 100644 --- a/packages/open-api/hoarder-openapi-spec.json +++ b/packages/open-api/hoarder-openapi-spec.json @@ -292,6 +292,57 @@ "Cursor": { "type": "string" }, + "Highlight": { + "type": "object", + "properties": { + "bookmarkId": { + "type": "string" + }, + "startOffset": { + "type": "number" + }, + "endOffset": { + "type": "number" + }, + "color": { + "type": "string", + "enum": [ + "yellow", + "red", + "green", + "blue" + ], + "default": "yellow" + }, + "text": { + "type": "string", + "nullable": true + }, + "note": { + "type": "string", + "nullable": true + }, + "id": { + "type": "string" + }, + "userId": { + "type": "string" + }, + "createdAt": { + "type": "string" + } + }, + "required": [ + "bookmarkId", + "startOffset", + "endOffset", + "text", + "note", + "id", + "userId", + "createdAt" + ] + }, "List": { "type": "object", "properties": { @@ -870,6 +921,48 @@ } } }, + "/bookmarks/{bookmarkId}/highlights": { + "get": { + "description": "Get highlights of a bookmark", + "summary": "Get highlights of a bookmark", + "tags": [ + "Bookmarks" + ], + "security": [ + { + "bearerAuth": [] + } + ], + "parameters": [ + { + "$ref": "#/components/parameters/BookmarkId" + } + ], + "responses": { + "200": { + "description": "The list of highlights", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "highlights": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Highlight" + } + } + }, + "required": [ + "highlights" + ] + } + } + } + } + } + } + }, "/lists": { "get": { "description": "Get all lists", diff --git a/packages/open-api/lib/bookmarks.ts b/packages/open-api/lib/bookmarks.ts index 0ddf921e..12a122fa 100644 --- a/packages/open-api/lib/bookmarks.ts +++ b/packages/open-api/lib/bookmarks.ts @@ -12,6 +12,7 @@ import { } from "@hoarder/shared/types/bookmarks"; import { BearerAuth } from "./common"; +import { HighlightSchema } from "./highlights"; import { BookmarkSchema, PaginatedBookmarksSchema, @@ -217,3 +218,25 @@ registry.registerPath({ }, }, }); + +registry.registerPath({ + method: "get", + path: "/bookmarks/{bookmarkId}/highlights", + description: "Get highlights of a bookmark", + summary: "Get highlights of a bookmark", + tags: ["Bookmarks"], + security: [{ [BearerAuth.name]: [] }], + request: { + params: z.object({ bookmarkId: BookmarkIdSchema }), + }, + responses: { + 200: { + description: "The list of highlights", + content: { + "application/json": { + schema: z.object({ highlights: z.array(HighlightSchema) }), + }, + }, + }, + }, +}); diff --git a/packages/open-api/lib/highlights.ts b/packages/open-api/lib/highlights.ts new file mode 100644 index 00000000..fc4c2aed --- /dev/null +++ b/packages/open-api/lib/highlights.ts @@ -0,0 +1,162 @@ +import { + extendZodWithOpenApi, + OpenAPIRegistry, +} from "@asteasolutions/zod-to-openapi"; +import { z } from "zod"; + +import { + zHighlightSchema, + zNewHighlightSchema, + zUpdateHighlightSchema, +} from "@hoarder/shared/types/highlights"; + +import { BearerAuth } from "./common"; +import { PaginationSchema } from "./pagination"; + +export const registry = new OpenAPIRegistry(); +extendZodWithOpenApi(z); + +export const HighlightSchema = zHighlightSchema.openapi("Highlight"); + +export const PaginatedHighlightsSchema = z + .object({ + highlights: z.array(HighlightSchema), + nextCursor: z.string().nullable(), + }) + .openapi("PaginatedHighlights"); + +export const HighlightIdSchema = registry.registerParameter( + "HighlightId", + z.string().openapi({ + param: { + name: "highlightId", + in: "path", + }, + example: "ieidlxygmwj87oxz5hxttoc8", + }), +); + +registry.registerPath({ + method: "get", + path: "/highlights", + description: "Get all highlights", + summary: "Get all highlights", + tags: ["Highlights"], + security: [{ [BearerAuth.name]: [] }], + request: { + query: PaginationSchema, + }, + responses: { + 200: { + description: "Object with all highlights data.", + content: { + "application/json": { + schema: PaginatedHighlightsSchema, + }, + }, + }, + }, +}); + +registry.registerPath({ + method: "post", + path: "/highlights", + description: "Create a new highlight", + summary: "Create a new highlight", + tags: ["Highlights"], + security: [{ [BearerAuth.name]: [] }], + request: { + body: { + description: "The highlight to create", + content: { + "application/json": { + schema: zNewHighlightSchema, + }, + }, + }, + }, + responses: { + 201: { + description: "The created highlight", + content: { + "application/json": { + schema: HighlightSchema, + }, + }, + }, + }, +}); +registry.registerPath({ + method: "get", + path: "/highlights/{highlightId}", + description: "Get highlight by its id", + summary: "Get a single highlight", + tags: ["Highlights"], + security: [{ [BearerAuth.name]: [] }], + request: { + params: z.object({ highlightId: HighlightIdSchema }), + }, + responses: { + 200: { + description: "Object with highlight data.", + content: { + "application/json": { + schema: HighlightSchema, + }, + }, + }, + }, +}); + +registry.registerPath({ + method: "delete", + path: "/highlights/{highlightId}", + description: "Delete highlight by its id", + summary: "Delete a highlight", + tags: ["Highlights"], + security: [{ [BearerAuth.name]: [] }], + request: { + params: z.object({ highlightId: HighlightIdSchema }), + }, + responses: { + 200: { + description: "The deleted highlight", + content: { + "application/json": { + schema: HighlightSchema, + }, + }, + }, + }, +}); + +registry.registerPath({ + method: "patch", + path: "/highlights/{highlightId}", + description: "Update highlight by its id", + summary: "Update a highlight", + tags: ["Highlights"], + security: [{ [BearerAuth.name]: [] }], + request: { + params: z.object({ highlightId: HighlightIdSchema }), + body: { + description: + "The data to update. Only the fields you want to update need to be provided.", + content: { + "application/json": { + schema: zUpdateHighlightSchema.omit({ highlightId: true }), + }, + }, + }, + }, + responses: { + 200: { + description: "The updated highlight", + content: { + "application/json": { + schema: HighlightSchema, + }, + }, + }, + }, +}); |
