-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.ts
More file actions
28 lines (22 loc) · 1.03 KB
/
api.ts
File metadata and controls
28 lines (22 loc) · 1.03 KB
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
const VERSION_PREFIX_PATTERN = /^\/v\d+(?=\/|$)/
export const SEVALLA_API_BASE = 'https://api.sevalla.com'
export const SEVALLA_API_PREFIX = '/v3'
export const SEVALLA_SPEC_URL = `${SEVALLA_API_BASE}${SEVALLA_API_PREFIX}/openapi.json`
export const normalizeApiPath = (path: string): string => {
const normalized = path.replace(VERSION_PREFIX_PATTERN, '')
return normalized || '/'
}
export const prependApiPrefix = (path: string): string => {
return `${SEVALLA_API_PREFIX}${normalizeApiPath(path)}`
}
export const createAuthenticatedFetch = (token: string) => {
return async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
const rawUrl = typeof input === 'string' || input instanceof URL ? input.toString() : input.url
const url = new URL(rawUrl)
url.pathname = prependApiPrefix(url.pathname)
const headers = new Headers(init?.headers)
headers.set('Authorization', `Bearer ${token}`)
headers.set('Content-Type', 'application/json')
return fetch(url.toString(), { ...init, headers })
}
}