-
Notifications
You must be signed in to change notification settings - Fork 0
feat(website): add API wrapper for collections backend API #1081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
35094aa
Move backendApi to src/ top level alongside lapis and covspectrum
fhennig ed0496b
Add collection types and backend service methods
fhennig af4bb87
Change collection and variant IDs from String to number
fhennig 104680f
format
fhennig 09817ec
Extract shared backend proxy, add collections proxy route
fhennig 132454b
Drop redundant userId args from collection service methods
fhennig 2b217f7
Drop userId from subscription call sites
fhennig 0b4e795
add a bit of docs
fhennig 32c35b4
Update MutationListDefinition schema to match simplified backend type
fhennig d2e619c
Adapt Collection types to renamed FilterObject backend API
fhennig 5acc319
Move backendProxy.ts from pages/api/ to backendApi/
fhennig 90d170b
Split collections catch-all route into explicit per-endpoint files
fhennig e951f78
Add backendService tests for getCollections
fhennig 93f4fb8
Replace proxyToBackendOptionalAuth with proxyToBackendNoAuth for coll…
fhennig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { getSession } from 'auth-astro/server'; | ||
|
|
||
| import { getBackendHost } from '../config.ts'; | ||
| import { getInstanceLogger } from '../logger.ts'; | ||
| import type { ProblemDetail } from '../types/ProblemDetail.ts'; | ||
| import { getErrorLogMessage } from '../util/getErrorLogMessage.ts'; | ||
|
|
||
| const logger = getInstanceLogger('BackendProxy'); | ||
|
|
||
| const API_PATHNAME_LENGTH = '/api'.length; | ||
|
|
||
| /** | ||
| * Calls the backend. If the user is logged in, the user ID is added from the session. | ||
| * This proxying through the frontend server is used, so we do the user login handling | ||
| * in here, instead of in the backend. | ||
| */ | ||
| export async function proxyToBackend({ request }: { request: Request }): Promise<Response> { | ||
| const session = await getSession(request); | ||
|
|
||
| if (session?.user?.id === undefined) { | ||
| return getUnauthorizedResponse(request.url); | ||
| } | ||
|
fengelniederhammer marked this conversation as resolved.
|
||
|
|
||
| return proxyRequest(request, session.user.id); | ||
| } | ||
|
|
||
| /** | ||
| * Proxies the request to the backend without any user ID, regardless of login state. | ||
| */ | ||
| export async function proxyToBackendNoAuth({ request }: { request: Request }): Promise<Response> { | ||
| return proxyRequest(request, undefined); | ||
| } | ||
|
|
||
| async function proxyRequest(request: Request, userId: string | undefined): Promise<Response> { | ||
| const backendUrl = getBackendUrl(request, userId); | ||
|
|
||
| try { | ||
| const response = await fetch(backendUrl, request); | ||
|
|
||
| return new Response(response.body, { | ||
| status: response.status, | ||
| headers: response.headers, | ||
| }); | ||
| } catch (error) { | ||
| logger.error(getErrorLogMessage(error)); | ||
| return getInternalErrorResponse(request.url); | ||
| } | ||
| } | ||
|
|
||
| function getBackendUrl(request: Request, userId: string | undefined) { | ||
| const backendEndpoint = new URL(request.url).pathname.slice(API_PATHNAME_LENGTH); | ||
| const backendUrl = new URL(backendEndpoint, getBackendHost()); | ||
|
|
||
| new URL(request.url).searchParams.forEach((value, key) => { | ||
| backendUrl.searchParams.set(key, value); | ||
| }); | ||
|
|
||
| if (userId !== undefined) { | ||
| backendUrl.searchParams.set('userId', userId); | ||
| } | ||
|
|
||
| return backendUrl; | ||
| } | ||
|
|
||
| const getUnauthorizedResponse = (requestUrl: string) => { | ||
| const response: ProblemDetail = { | ||
| title: 'Unauthorized', | ||
| detail: "You're not authorized to access this resource", | ||
| status: 401, | ||
| instance: requestUrl, | ||
| }; | ||
|
|
||
| return Response.json(response, { | ||
| status: 401, | ||
| headers: { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| 'Content-Type': 'application/json', | ||
|
fhennig marked this conversation as resolved.
|
||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const getInternalErrorResponse = (requestUrl: string) => { | ||
| const response: ProblemDetail = { | ||
| title: 'Internal Server Error', | ||
| detail: 'Failed to connect the backend service', | ||
| status: 500, | ||
| instance: requestUrl, | ||
| }; | ||
|
|
||
| return Response.json(response, { | ||
| status: 500, | ||
| headers: { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.