Skip to content

Commit b53e29a

Browse files
committed
feat(api): Use baseURL instead of hostName
following the style of `jsr:@openai/openai`
1 parent 98dae98 commit b53e29a

File tree

10 files changed

+28
-31
lines changed

10 files changed

+28
-31
lines changed

api/pages/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const makeGetRequest = <R extends Response | undefined>(
5353
project: string,
5454
options?: ListPagesOption<R>,
5555
): Request => {
56-
const { sid, hostName, sort, limit, skip } = setDefaults(
56+
const { sid, baseURL, sort, limit, skip } = setDefaults(
5757
options ?? {},
5858
);
5959
const params = new URLSearchParams();
@@ -62,7 +62,7 @@ export const makeGetRequest = <R extends Response | undefined>(
6262
if (skip !== undefined) params.append("skip", `${skip}`);
6363

6464
return new Request(
65-
`https://${hostName}/api/pages/${project}?${params}`,
65+
`${baseURL}api/pages/${project}?${params}`,
6666
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
6767
);
6868
};

api/pages/project/replace/links.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export const makePostRequest = <R extends Response | undefined>(
2222
to: string,
2323
init?: ExtendedOptions<R>,
2424
): Request => {
25-
const { sid, hostName, csrf } = setDefaults(init ?? {});
25+
const { sid, baseURL, csrf } = setDefaults(init ?? {});
2626

2727
return new Request(
28-
`https://${hostName}/api/pages/${project}/replace/links`,
28+
`${baseURL}api/pages/${project}/replace/links`,
2929
{
3030
method: "POST",
3131
headers: {

api/pages/project/search/query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export const makeGetRequest = <R extends Response | undefined>(
2020
query: string,
2121
options?: BaseOptions<R>,
2222
): Request => {
23-
const { sid, hostName } = setDefaults(options ?? {});
23+
const { sid, baseURL } = setDefaults(options ?? {});
2424

2525
return new Request(
26-
`https://${hostName}/api/pages/${project}/search/query?q=${
26+
`${baseURL}api/pages/${project}/search/query?q=${
2727
encodeURIComponent(query)
2828
}`,
2929
sid ? { headers: { Cookie: cookie(sid) } } : undefined,

api/pages/project/search/titles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ export const makeGetRequest = <R extends Response | undefined>(
3131
project: string,
3232
options?: GetLinksOptions<R>,
3333
): Request => {
34-
const { sid, hostName, followingId } = setDefaults(options ?? {});
34+
const { sid, baseURL, followingId } = setDefaults(options ?? {});
3535

3636
return new Request(
37-
`https://${hostName}/api/pages/${project}/search/titles${
37+
`${baseURL}api/pages/${project}/search/titles${
3838
followingId ? `?followingId=${followingId}` : ""
3939
}`,
4040
sid ? { headers: { Cookie: cookie(sid) } } : undefined,

api/pages/project/title.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ export const makeGetRequest = <R extends Response | undefined>(
3131
title: string,
3232
options?: GetPageOption<R>,
3333
): Request => {
34-
const { sid, hostName, followRename, projects } = setDefaults(options ?? {});
34+
const { sid, baseURL, followRename, projects } = setDefaults(options ?? {});
3535

3636
const params = new URLSearchParams([
3737
["followRename", `${followRename ?? true}`],
3838
...(projects?.map?.((id) => ["projects", id]) ?? []),
3939
]);
4040

4141
return new Request(
42-
`https://${hostName}/api/pages/${project}/${
43-
encodeTitleURI(title)
44-
}?${params}`,
42+
`${baseURL}api/pages/${project}/${encodeTitleURI(title)}?${params}`,
4543
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
4644
);
4745
};

api/pages/project/title/icon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export const makeGetRequest = <R extends Response | undefined>(
2727
title: string,
2828
options?: GetIconOption<R>,
2929
): Request => {
30-
const { sid, hostName, followRename } = setDefaults(options ?? {});
30+
const { sid, baseURL, followRename } = setDefaults(options ?? {});
3131

3232
return new Request(
33-
`https://${hostName}/api/pages/${project}/${
33+
`${baseURL}api/pages/${project}/${
3434
encodeTitleURI(title)
3535
}/icon?followRename=${followRename ?? true}`,
3636
sid ? { headers: { Cookie: cookie(sid) } } : undefined,

api/pages/project/title/text.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export const makeGetRequest = <R extends Response | undefined>(
2727
title: string,
2828
options?: GetTextOption<R>,
2929
): Request => {
30-
const { sid, hostName, followRename } = setDefaults(options ?? {});
30+
const { sid, baseURL, followRename } = setDefaults(options ?? {});
3131

3232
return new Request(
33-
`https://${hostName}/api/pages/${project}/${
33+
`${baseURL}api/pages/${project}/${
3434
encodeTitleURI(title)
3535
}/text?followRename=${followRename ?? true}`,
3636
sid ? { headers: { Cookie: cookie(sid) } } : undefined,

api/projects/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export const makeGetRequest = <R extends Response | undefined>(
1919
project: string,
2020
options?: BaseOptions<R>,
2121
): Request => {
22-
const { sid, hostName } = setDefaults(options ?? {});
22+
const { sid, baseURL } = setDefaults(options ?? {});
2323

2424
return new Request(
25-
`https://${hostName}/api/projects/${project}`,
25+
`${baseURL}api/projects/${project}`,
2626
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
2727
);
2828
};

api/users/me.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import { cookie } from "../../rest/auth.ts";
1414
export const makeGetRequest = <R extends Response | undefined>(
1515
init?: BaseOptions<R>,
1616
): Request => {
17-
const { sid, hostName } = setDefaults(init ?? {});
17+
const { sid, baseURL } = setDefaults(init ?? {});
1818
return new Request(
19-
`https://${hostName}/api/users/me`,
19+
`${baseURL}api/users/me`,
2020
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
2121
);
2222
};

util.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,11 @@ export interface BaseOptions<R extends Response | undefined> {
3636
*/
3737
fetch?: Fetch<R>;
3838

39-
/** Domain for REST API endpoints
39+
/** Base URL for REST API endpoints
4040
*
41-
* Configurable host name for API requests. This allows using the library
42-
* with self-hosted Scrapbox instances or other custom deployments that
43-
* don't use the default scrapbox.io domain.
44-
*
45-
* @default {"scrapbox.io"}
41+
* @default {"https://scrapbox.io/"}
4642
*/
47-
hostName?: string;
43+
baseURL?: string;
4844
}
4945

5046
/** Options for Gyazo API which requires OAuth */
@@ -83,8 +79,11 @@ export const setDefaults = <
8379
T extends BaseOptions<any> = BaseOptions<Response>,
8480
>(
8581
options: T,
86-
): Omit<T, "fetch" | "hostName"> & Required<Pick<T, "fetch" | "hostName">> => {
87-
const { fetch = globalThis.fetch, hostName = "scrapbox.io", ...rest } =
88-
options;
89-
return { fetch, hostName, ...rest };
82+
): Omit<T, "fetch" | "baseURL"> & Required<Pick<T, "fetch" | "baseURL">> => {
83+
const {
84+
fetch = globalThis.fetch,
85+
baseURL = "https://scrapbox.io/",
86+
...rest
87+
} = options;
88+
return { fetch, baseURL, ...rest };
9089
};

0 commit comments

Comments
 (0)