@@ -6,39 +6,27 @@ import type {
6
6
NotLoggedInError ,
7
7
NotPrivilegeError ,
8
8
} 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" ;
26
13
/** projectにページをインポートする
27
14
*
28
15
* @param project - インポート先のprojectの名前
29
16
* @param data - インポートするページデータ
30
17
*/
31
- export async function importPages (
18
+ export const importPages = async (
32
19
project : string ,
33
20
data : ImportedData < boolean > ,
34
- { sid , csrf } : ImportInit ,
21
+ init : ExtendedOptions ,
35
22
) : Promise <
36
23
Result < string , ErrorLike >
37
- > {
24
+ > => {
38
25
if ( data . pages . length === 0 ) {
39
26
return { ok : true , value : "No pages to import." } ;
40
27
}
41
28
29
+ const { sid, hostName, fetch, csrf } = setDefaults ( init ?? { } ) ;
42
30
const formData = new FormData ( ) ;
43
31
formData . append (
44
32
"import-file" ,
@@ -47,90 +35,80 @@ export async function importPages(
47
35
} ) ,
48
36
) ;
49
37
formData . append ( "name" , "undefined" ) ;
38
+ const path = `https://${ hostName } /api/page-data/import/${ project } .json` ;
50
39
51
- csrf ??= await getCSRFToken ( sid ) ;
52
-
53
- const path = `https://scrapbox.io/api/page-data/import/${ project } .json` ;
54
40
const res = await fetch (
55
41
path ,
56
42
{
57
43
method : "POST" ,
58
44
headers : {
59
- Cookie : cookie ( sid ) ,
45
+ ... ( sid ? { Cookie : cookie ( sid ) } : { } ) ,
60
46
Accept : "application/json, text/plain, */*" ,
61
- "X-CSRF-TOKEN" : csrf ,
47
+ "X-CSRF-TOKEN" : csrf ?? await getCSRFToken ( init ) ,
62
48
} ,
63
49
body : formData ,
64
50
} ,
65
51
) ;
66
52
67
53
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 ) ;
72
56
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
+ } ) ;
77
62
}
78
63
return { ok : false , value } ;
79
64
}
65
+
80
66
const { message } = ( await res . json ( ) ) as { message : string } ;
81
67
return { ok : true , value : message } ;
82
- }
68
+ } ;
83
69
84
70
/** `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 {
87
73
/** whether to includes metadata */ metadata : withMetadata ;
88
74
}
89
75
/** projectの全ページをエクスポートする
90
76
*
91
77
* @param project exportしたいproject
92
78
*/
93
- export async function exportPages < withMetadata extends true | false > (
79
+ export const exportPages = async < withMetadata extends true | false > (
94
80
project : string ,
95
- { sid , metadata } : ExportInit < withMetadata > ,
81
+ init : ExportInit < withMetadata > ,
96
82
) : Promise <
97
83
Result <
98
84
ExportedData < withMetadata > ,
99
85
NotFoundError | NotPrivilegeError | NotLoggedInError
100
86
>
101
- > {
87
+ > => {
88
+ const { sid, hostName, fetch, metadata } = setDefaults ( init ?? { } ) ;
102
89
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 } ` ;
104
91
const res = await fetch (
105
92
path ,
106
- {
107
- headers : {
108
- Cookie : cookie ( sid ) ,
109
- } ,
110
- } ,
93
+ sid ? { headers : { Cookie : cookie ( sid ) } } : undefined ,
111
94
) ;
112
95
113
96
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 ) ;
123
99
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
+ } ) ;
128
105
}
129
106
return {
130
107
ok : false ,
131
- value,
108
+ value : value as NotFoundError | NotPrivilegeError | NotLoggedInError ,
132
109
} ;
133
110
}
111
+
134
112
const value = ( await res . json ( ) ) as ExportedData < withMetadata > ;
135
113
return { ok : true , value } ;
136
- }
114
+ } ;
0 commit comments