@@ -6,39 +6,27 @@ import type {
66 NotLoggedInError ,
77 NotPrivilegeError ,
88} from "../deps/scrapbox.ts" ;
9- import {
10- cookie ,
11- getCSRFToken ,
12- makeCustomError ,
13- tryToErrorLike ,
14- } from "./utils.ts" ;
15- import type { Result } from "./utils.ts" ;
16-
17- /** `importPages`の認証情報 */
18- export interface ImportInit {
19- /** connect.sid */ sid : string ;
20- /** CSRF token
21- *
22- * If it isn't set, automatically get CSRF token from scrapbox.io server.
23- */
24- csrf ?: string ;
25- }
9+ import { cookie , getCSRFToken } from "./auth.ts" ;
10+ import { UnexpectedResponseError } from "./error.ts" ;
11+ import { tryToErrorLike } from "../is.ts" ;
12+ import { BaseOptions , ExtendedOptions , Result , setDefaults } from "./util.ts" ;
2613/** projectにページをインポートする
2714 *
2815 * @param project - インポート先のprojectの名前
2916 * @param data - インポートするページデータ
3017 */
31- export async function importPages (
18+ export const importPages = async (
3219 project : string ,
3320 data : ImportedData < boolean > ,
34- { sid , csrf } : ImportInit ,
21+ init : ExtendedOptions ,
3522) : Promise <
3623 Result < string , ErrorLike >
37- > {
24+ > => {
3825 if ( data . pages . length === 0 ) {
3926 return { ok : true , value : "No pages to import." } ;
4027 }
4128
29+ const { sid, hostName, fetch, csrf } = setDefaults ( init ?? { } ) ;
4230 const formData = new FormData ( ) ;
4331 formData . append (
4432 "import-file" ,
@@ -47,90 +35,80 @@ export async function importPages(
4735 } ) ,
4836 ) ;
4937 formData . append ( "name" , "undefined" ) ;
38+ const path = `https://${ hostName } /api/page-data/import/${ project } .json` ;
5039
51- csrf ??= await getCSRFToken ( sid ) ;
52-
53- const path = `https://scrapbox.io/api/page-data/import/${ project } .json` ;
5440 const res = await fetch (
5541 path ,
5642 {
5743 method : "POST" ,
5844 headers : {
59- Cookie : cookie ( sid ) ,
45+ ... ( sid ? { Cookie : cookie ( sid ) } : { } ) ,
6046 Accept : "application/json, text/plain, */*" ,
61- "X-CSRF-TOKEN" : csrf ,
47+ "X-CSRF-TOKEN" : csrf ?? await getCSRFToken ( init ) ,
6248 } ,
6349 body : formData ,
6450 } ,
6551 ) ;
6652
6753 if ( ! res . ok ) {
68- if ( res . status === 503 ) {
69- throw makeCustomError ( "ServerError" , "503 Service Unavailable" ) ;
70- }
71- const value = tryToErrorLike ( await res . text ( ) ) ;
54+ const text = await res . json ( ) ;
55+ const value = tryToErrorLike ( text ) ;
7256 if ( ! value ) {
73- throw makeCustomError (
74- "UnexpectedError" ,
75- `Unexpected error has occuerd when fetching "${ path } "` ,
76- ) ;
57+ throw new UnexpectedResponseError ( {
58+ path : new URL ( path ) ,
59+ ...res ,
60+ body : await res . text ( ) ,
61+ } ) ;
7762 }
7863 return { ok : false , value } ;
7964 }
65+
8066 const { message } = ( await res . json ( ) ) as { message : string } ;
8167 return { ok : true , value : message } ;
82- }
68+ } ;
8369
8470/** `exportPages`の認証情報 */
85- export interface ExportInit < withMetadata extends true | false > {
86- /** connect.sid */ sid : string ;
71+ export interface ExportInit < withMetadata extends true | false >
72+ extends BaseOptions {
8773 /** whether to includes metadata */ metadata : withMetadata ;
8874}
8975/** projectの全ページをエクスポートする
9076 *
9177 * @param project exportしたいproject
9278 */
93- export async function exportPages < withMetadata extends true | false > (
79+ export const exportPages = async < withMetadata extends true | false > (
9480 project : string ,
95- { sid , metadata } : ExportInit < withMetadata > ,
81+ init : ExportInit < withMetadata > ,
9682) : Promise <
9783 Result <
9884 ExportedData < withMetadata > ,
9985 NotFoundError | NotPrivilegeError | NotLoggedInError
10086 >
101- > {
87+ > => {
88+ const { sid, hostName, fetch, metadata } = setDefaults ( init ?? { } ) ;
10289 const path =
103- `https://scrapbox.io /api/page-data/export/${ project } .json?metadata=${ metadata } ` ;
90+ `https://${ hostName } /api/page-data/export/${ project } .json?metadata=${ metadata } ` ;
10491 const res = await fetch (
10592 path ,
106- {
107- headers : {
108- Cookie : cookie ( sid ) ,
109- } ,
110- } ,
93+ sid ? { headers : { Cookie : cookie ( sid ) } } : undefined ,
11194 ) ;
11295
11396 if ( ! res . ok ) {
114- const error = ( await res . json ( ) ) ;
115- return { ok : false , ...error } ;
116- }
117- if ( ! res . ok ) {
118- const value = tryToErrorLike ( await res . text ( ) ) as
119- | false
120- | NotFoundError
121- | NotPrivilegeError
122- | NotLoggedInError ;
97+ const text = await res . json ( ) ;
98+ const value = tryToErrorLike ( text ) ;
12399 if ( ! value ) {
124- throw makeCustomError (
125- "UnexpectedError" ,
126- `Unexpected error has occuerd when fetching "${ path } "` ,
127- ) ;
100+ throw new UnexpectedResponseError ( {
101+ path : new URL ( path ) ,
102+ ...res ,
103+ body : await res . text ( ) ,
104+ } ) ;
128105 }
129106 return {
130107 ok : false ,
131- value,
108+ value : value as NotFoundError | NotPrivilegeError | NotLoggedInError ,
132109 } ;
133110 }
111+
134112 const value = ( await res . json ( ) ) as ExportedData < withMetadata > ;
135113 return { ok : true , value } ;
136- }
114+ } ;
0 commit comments