-
Notifications
You must be signed in to change notification settings - Fork 172
/
next-drupal-base.ts
534 lines (445 loc) · 13.9 KB
/
next-drupal-base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import { stringify } from "qs"
import { JsonApiErrors } from "./jsonapi-errors"
import { logger as defaultLogger } from "./logger"
import type {
AccessToken,
BaseUrl,
EndpointSearchParams,
FetchOptions,
JsonApiResponse,
Locale,
Logger,
NextDrupalAuth,
NextDrupalAuthAccessToken,
NextDrupalAuthClientIdSecret,
NextDrupalAuthUsernamePassword,
NextDrupalBaseOptions,
PathPrefix,
} from "./types"
const DEFAULT_API_PREFIX = ""
const DEFAULT_FRONT_PAGE = "/home"
const DEFAULT_WITH_AUTH = false
// From simple_oauth.
const DEFAULT_AUTH_URL = "/oauth/token"
// See https://jsonapi.org/format/#content-negotiation.
const DEFAULT_HEADERS = {
"Content-Type": "application/json",
Accept: "application/json",
}
export class NextDrupalBase {
accessToken?: NextDrupalBaseOptions["accessToken"]
baseUrl: BaseUrl
fetcher?: NextDrupalBaseOptions["fetcher"]
frontPage: string
isDebugEnabled: boolean
logger: Logger
withAuth: boolean
private _apiPrefix: string
private _auth?: NextDrupalAuth
private _headers: Headers
private _token?: AccessToken
private _tokenExpiresOn?: number
private _tokenRequestDetails?: NextDrupalAuthClientIdSecret
/**
* Instantiates a new NextDrupalBase.
*
* const client = new NextDrupalBase(baseUrl)
*
* @param {baseUrl} baseUrl The baseUrl of your Drupal site. Do not add the /jsonapi suffix.
* @param {options} options Options for NextDrupalBase.
*/
constructor(baseUrl: BaseUrl, options: NextDrupalBaseOptions = {}) {
if (!baseUrl || typeof baseUrl !== "string") {
throw new Error("The 'baseUrl' param is required.")
}
const {
accessToken,
apiPrefix = DEFAULT_API_PREFIX,
auth,
debug = false,
fetcher,
frontPage = DEFAULT_FRONT_PAGE,
headers = DEFAULT_HEADERS,
logger = defaultLogger,
withAuth = DEFAULT_WITH_AUTH,
} = options
this.accessToken = accessToken
this.apiPrefix = apiPrefix
this.auth = auth
this.baseUrl = baseUrl
this.fetcher = fetcher
this.frontPage = frontPage
this.isDebugEnabled = !!debug
this.headers = headers
this.logger = logger
this.withAuth = withAuth
this.debug("Debug mode is on.")
}
set apiPrefix(apiPrefix: string) {
this._apiPrefix =
apiPrefix === "" || apiPrefix.startsWith("/")
? apiPrefix
: `/${apiPrefix}`
}
get apiPrefix() {
return this._apiPrefix
}
set auth(auth: NextDrupalAuth) {
if (typeof auth === "object") {
const checkUsernamePassword = auth as NextDrupalAuthUsernamePassword
const checkAccessToken = auth as NextDrupalAuthAccessToken
const checkClientIdSecret = auth as NextDrupalAuthClientIdSecret
if (
checkUsernamePassword.username !== undefined ||
checkUsernamePassword.password !== undefined
) {
if (
!checkUsernamePassword.username ||
!checkUsernamePassword.password
) {
throw new Error(
"'username' and 'password' are required for auth. See https://next-drupal.org/docs/client/auth"
)
}
} else if (
checkAccessToken.access_token !== undefined ||
checkAccessToken.token_type !== undefined
) {
if (!checkAccessToken.access_token || !checkAccessToken.token_type) {
throw new Error(
"'access_token' and 'token_type' are required for auth. See https://next-drupal.org/docs/client/auth"
)
}
} else if (
!checkClientIdSecret.clientId ||
!checkClientIdSecret.clientSecret
) {
throw new Error(
"'clientId' and 'clientSecret' are required for auth. See https://next-drupal.org/docs/client/auth"
)
}
this._auth = {
...(isClientIdSecretAuth(auth) ? { url: DEFAULT_AUTH_URL } : {}),
...auth,
}
} else {
this._auth = auth
}
}
get auth() {
return this._auth
}
set headers(headers: HeadersInit) {
this._headers = new Headers(headers)
}
get headers() {
return this._headers
}
set token(token: AccessToken) {
this._token = token
this._tokenExpiresOn = Date.now() + token.expires_in * 1000
}
get token() {
return this._token
}
async fetch(
input: RequestInfo,
{ withAuth, ...init }: FetchOptions = {}
): Promise<Response> {
init.credentials = "include"
// Merge the init.headers with this.headers
const headers = new Headers(this.headers)
if (init?.headers) {
const initHeaders = new Headers(init?.headers)
for (const key of initHeaders.keys()) {
headers.set(key, initHeaders.get(key))
}
}
// Set Authorization header.
if (withAuth) {
headers.set(
"Authorization",
await this.getAuthorizationHeader(
withAuth === true ? this.auth : withAuth
)
)
}
init.headers = headers
if (typeof input === "string" && input.startsWith("/")) {
input = `${this.baseUrl}${input}`
}
if (this.fetcher) {
this.debug(`Using custom fetcher, fetching: ${input}`)
return await this.fetcher(input, init)
}
this.debug(`Using default fetch, fetching: ${input}`)
return await fetch(input, init)
}
async getAuthorizationHeader(auth: NextDrupalAuth) {
let header: string
if (isBasicAuth(auth)) {
const basic = Buffer.from(`${auth.username}:${auth.password}`).toString(
"base64"
)
header = `Basic ${basic}`
this.debug("Using basic authorization header.")
} else if (isClientIdSecretAuth(auth)) {
// Fetch an access token and add it to the request. getAccessToken()
// throws an error if it fails to get an access token.
const token = await this.getAccessToken(auth)
header = `Bearer ${token.access_token}`
this.debug(
"Using access token authorization header retrieved from Client Id/Secret."
)
} else if (isAccessTokenAuth(auth)) {
header = `${auth.token_type} ${auth.access_token}`
this.debug("Using access token authorization header.")
} else if (typeof auth === "string") {
header = auth
this.debug("Using custom authorization header.")
} else if (typeof auth === "function") {
header = auth()
this.debug("Using custom authorization callback.")
} else {
throw new Error(
"auth is not configured. See https://next-drupal.org/docs/client/auth"
)
}
return header
}
buildUrl(path: string, searchParams?: EndpointSearchParams): URL {
const url = new URL(path, this.baseUrl)
const search =
// Handle DrupalJsonApiParams objects.
searchParams &&
typeof searchParams === "object" &&
"getQueryObject" in searchParams
? searchParams.getQueryObject()
: searchParams
if (search) {
// Use stringify instead of URLSearchParams for nested params.
url.search = stringify(search)
}
return url
}
// async so subclasses can query for endpoint discovery.
async buildEndpoint({
locale = "",
path = "",
searchParams,
}: {
locale?: string
path?: string
searchParams?: EndpointSearchParams
} = {}): Promise<string> {
const localeSegment = locale ? `/${locale}` : ""
if (path && !path.startsWith("/")) {
path = `/${path}`
}
return this.buildUrl(
`${localeSegment}${this.apiPrefix}${path}`,
searchParams
).toString()
}
constructPathFromSegment(
segment: string | string[],
options: {
locale?: Locale
defaultLocale?: Locale
pathPrefix?: PathPrefix
} = {}
) {
let { pathPrefix = "" } = options
const { locale, defaultLocale } = options
// Ensure pathPrefix starts with a "/" and does not end with a "/".
if (pathPrefix) {
if (!pathPrefix?.startsWith("/")) {
pathPrefix = `/${options.pathPrefix}`
}
if (pathPrefix.endsWith("/")) {
pathPrefix = pathPrefix.slice(0, -1)
}
}
// If the segment is given as an array of segments, join the parts.
if (!Array.isArray(segment)) {
segment = segment ? [segment] : []
}
segment = segment.map((part) => encodeURIComponent(part)).join("/")
if (!segment && !pathPrefix) {
// If no pathPrefix is given and the segment is empty, then the path
// should be the homepage.
segment = this.frontPage
}
// Ensure the segment starts with a "/" and does not end with a "/".
if (segment && !segment.startsWith("/")) {
segment = `/${segment}`
}
if (segment.endsWith("/")) {
segment = segment.slice(0, -1)
}
return this.addLocalePrefix(`${pathPrefix}${segment}`, {
locale,
defaultLocale,
})
}
addLocalePrefix(
path: string,
options: { locale?: Locale; defaultLocale?: Locale } = {}
) {
const { locale, defaultLocale } = options
if (!path.startsWith("/")) {
path = `/${path}`
}
let localePrefix = ""
if (locale && !path.startsWith(`/${locale}`) && locale !== defaultLocale) {
localePrefix = `/${locale}`
}
return `${localePrefix}${path}`
}
async getAccessToken(
clientIdSecret?: NextDrupalAuthClientIdSecret
): Promise<AccessToken> {
if (this.accessToken) {
return this.accessToken
}
let auth: NextDrupalAuthClientIdSecret
if (isClientIdSecretAuth(clientIdSecret)) {
auth = {
url: DEFAULT_AUTH_URL,
...clientIdSecret,
}
} else if (isClientIdSecretAuth(this.auth)) {
auth = { ...this.auth }
} else if (typeof this.auth === "undefined") {
throw new Error(
"auth is not configured. See https://next-drupal.org/docs/client/auth"
)
} else {
throw new Error(
`'clientId' and 'clientSecret' required. See https://next-drupal.org/docs/client/auth`
)
}
const url = this.buildUrl(auth.url)
// Ensure that the unexpired token was using the same scope and client
// credentials as the current request before re-using it.
if (
this.token &&
Date.now() < this._tokenExpiresOn &&
this._tokenRequestDetails?.clientId === auth?.clientId &&
this._tokenRequestDetails?.clientSecret === auth?.clientSecret &&
this._tokenRequestDetails?.scope === auth?.scope
) {
this.debug(`Using existing access token.`)
return this.token
}
this.debug(`Fetching new access token.`)
// Use BasicAuth to retrieve the access token.
const clientCredentials: NextDrupalAuthUsernamePassword = {
username: auth.clientId,
password: auth.clientSecret,
}
const body = new URLSearchParams({ grant_type: "client_credentials" })
if (auth?.scope) {
body.set("scope", auth.scope)
this.debug(`Using scope: ${auth.scope}`)
}
const response = await this.fetch(url.toString(), {
method: "POST",
headers: {
Authorization: await this.getAuthorizationHeader(clientCredentials),
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded",
},
body,
})
await this.throwIfJsonErrors(
response,
"Error while fetching new access token: "
)
const result: AccessToken = await response.json()
this.token = result
this._tokenRequestDetails = auth
return result
}
async validateDraftUrl(searchParams: URLSearchParams): Promise<Response> {
const path = searchParams.get("path")
this.debug(`Fetching draft url validation for ${path}.`)
// Fetch the headless CMS to check if the provided `path` exists
let response: Response
try {
// Validate the draft url.
const validateUrl = this.buildUrl("/next/draft-url").toString()
response = await this.fetch(validateUrl, {
method: "POST",
headers: {
Accept: "application/vnd.api+json",
"Content-Type": "application/json",
},
body: JSON.stringify(Object.fromEntries(searchParams.entries())),
})
} catch (error) {
response = new Response(JSON.stringify({ message: error.message }), {
status: 401,
})
}
this.debug(
response.status !== 200
? `Could not validate path, ${path}`
: `Validated path, ${path}`
)
return response
}
debug(message) {
this.isDebugEnabled && this.logger.debug(message)
}
async throwIfJsonErrors(response: Response, messagePrefix = "") {
if (!response?.ok) {
const errors = await this.getErrorsFromResponse(response)
throw new JsonApiErrors(errors, response.status, messagePrefix)
}
}
async getErrorsFromResponse(response: Response) {
const type = response.headers.get("content-type")
let error: JsonApiResponse | { message: string }
if (type === "application/json") {
error = await response.json()
if (error?.message) {
return error.message as string
}
}
// Construct error from response.
// Check for type to ensure this is a JSON:API formatted error.
// See https://jsonapi.org/format/#errors.
else if (type === "application/vnd.api+json") {
error = (await response.json()) as JsonApiResponse
if (error?.errors?.length) {
return error.errors
}
}
return response.statusText
}
}
export function isBasicAuth(
auth: NextDrupalAuth
): auth is NextDrupalAuthUsernamePassword {
return (
(auth as NextDrupalAuthUsernamePassword)?.username !== undefined &&
(auth as NextDrupalAuthUsernamePassword)?.password !== undefined
)
}
export function isAccessTokenAuth(
auth: NextDrupalAuth
): auth is NextDrupalAuthAccessToken {
return (
(auth as NextDrupalAuthAccessToken)?.access_token !== undefined &&
(auth as NextDrupalAuthAccessToken)?.token_type !== undefined
)
}
export function isClientIdSecretAuth(
auth: NextDrupalAuth
): auth is NextDrupalAuthClientIdSecret {
return (
(auth as NextDrupalAuthClientIdSecret)?.clientId !== undefined &&
(auth as NextDrupalAuthClientIdSecret)?.clientSecret !== undefined
)
}