Skip to content

Commit 05c5c9a

Browse files
committed
feat(api): Implement the /api/pages/:project/search/query endpoint
1 parent 8140a96 commit 05c5c9a

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

api/pages/project.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,6 @@ export async function* list(
135135
}
136136
}
137137

138-
export * as title from "./project/title.ts";
139138
export * as replace from "./project/replace.ts";
139+
export * as search from "./project/search.ts";
140+
export * as title from "./project/title.ts";

api/pages/project/search.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as query from "./search/query.ts";

api/pages/project/search/query.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import type {
2+
NotFoundError,
3+
NotLoggedInError,
4+
NotMemberError,
5+
SearchResult,
6+
} from "@cosense/types/rest";
7+
import type { ResponseOfEndpoint } from "../../../../targeted_response.ts";
8+
import { type BaseOptions, setDefaults } from "../../../../util.ts";
9+
import { cookie } from "../../../../rest/auth.ts";
10+
11+
/** Options for {@linkcode getPage} */
12+
export interface GetPageOption<R extends Response | undefined>
13+
extends BaseOptions<R> {
14+
/** use `followRename` */
15+
followRename?: boolean;
16+
17+
/** project ids to get External links */
18+
projects?: string[];
19+
}
20+
21+
/** Constructs a request for the `/api/pages/:project/search/query` endpoint
22+
*
23+
* @param project The name of the project to search within
24+
* @param query The search query string to match against pages
25+
* @param options - Additional configuration options
26+
* @returns A {@linkcode Request} object for fetching page data
27+
*/
28+
export const makeGetRequest = <R extends Response | undefined>(
29+
project: string,
30+
query: string,
31+
options?: BaseOptions<R>,
32+
): Request => {
33+
const { sid, hostName } = setDefaults(options ?? {});
34+
35+
return new Request(
36+
`https://${hostName}/api/pages/${project}/search/query?q=${
37+
encodeURIComponent(query)
38+
}`,
39+
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
40+
);
41+
};
42+
43+
/** Search for pages within a specific project
44+
*
45+
* @param project The name of the project to search within
46+
* @param query The search query string to match against pages
47+
* @param options Additional configuration options for the request
48+
* @returns A {@linkcode Response} object containing the search results
49+
*/
50+
export const get = <R extends Response | undefined = Response>(
51+
project: string,
52+
query: string,
53+
options?: BaseOptions<R>,
54+
): Promise<
55+
ResponseOfEndpoint<{
56+
200: SearchResult;
57+
404: NotFoundError;
58+
401: NotLoggedInError;
59+
403: NotMemberError;
60+
422: { message: string };
61+
}, R>
62+
> =>
63+
setDefaults(options ?? {}).fetch(
64+
makeGetRequest(project, query, options),
65+
) as Promise<
66+
ResponseOfEndpoint<{
67+
200: SearchResult;
68+
404: NotFoundError;
69+
401: NotLoggedInError;
70+
403: NotMemberError;
71+
422: { message: string };
72+
}, R>
73+
>;

deno.jsonc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
"./unstable-api": "./api.ts",
2525
"./unstable-api/pages": "./api/pages.ts",
2626
"./unstable-api/pages/project": "./api/pages/project.ts",
27+
"./unstable-api/pages/project/replace": "./api/pages/project/replace.ts",
28+
"./unstable-api/pages/project/replace/links": "./api/pages/project/replace/links.ts",
29+
"./unstable-api/pages/project/search": "./api/pages/project/search.ts",
30+
"./unstable-api/pages/project/search/query": "./api/pages/project/search/query.ts",
2731
"./unstable-api/pages/project/title": "./api/pages/project/title.ts",
2832
"./unstable-api/pages/project/title/text": "./api/pages/project/title/text.ts",
2933
"./unstable-api/pages/project/title/icon": "./api/pages/project/title/icon.ts",
30-
"./unstable-api/pages/project/replace": "./api/pages/project/replace.ts",
31-
"./unstable-api/pages/project/replace/links": "./api/pages/project/replace/links.ts",
3234
"./unstable-api/users": "./api/users.ts",
3335
"./unstable-api/users/me": "./api/users/me.ts"
3436
},

0 commit comments

Comments
 (0)