(params: AuthenticatorParams)
| 15 | }; |
| 16 | |
| 17 | export function createAuthenticator(params: AuthenticatorParams) { |
| 18 | return { |
| 19 | async whoami(): Promise<AuthPayload | null> { |
| 20 | try { |
| 21 | const res = await fetch(`${params.apiUrl}/users/me`, { |
| 22 | method: "GET", |
| 23 | headers: { |
| 24 | "X-API-Key": params.apiKey, |
| 25 | "Content-Type": "application/json", |
| 26 | }, |
| 27 | }); |
| 28 | |
| 29 | if (res.ok) { |
| 30 | const payload = await res.json(); |
| 31 | if (!payload?.email) { |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | return { |
| 36 | email: payload.email, |
| 37 | id: payload.id, |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | if (res.status >= 500 && res.status < 600) { |
| 42 | const originalErrorMessage = `Server error (${res.status}): ${res.statusText}. Please try again later.`; |
| 43 | |
| 44 | const cloudflareStatus = await checkCloudflareStatus(); |
| 45 | |
| 46 | if (!cloudflareStatus) { |
| 47 | throw new CLIError({ |
| 48 | message: originalErrorMessage, |
| 49 | docUrl: "connectionFailed", |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | if (cloudflareStatus.status.indicator !== "none") { |
| 54 | const cloudflareMessage = |
| 55 | formatCloudflareStatusMessage(cloudflareStatus); |
| 56 | throw new CLIError({ |
| 57 | message: cloudflareMessage, |
| 58 | docUrl: "connectionFailed", |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | throw new CLIError({ |
| 63 | message: originalErrorMessage, |
| 64 | docUrl: "connectionFailed", |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | return null; |
| 69 | } catch (error) { |
| 70 | if (error instanceof CLIError) { |
| 71 | throw error; |
| 72 | } |
| 73 | |
| 74 | const isNetworkError = |
no outgoing calls
no test coverage detected