fix(session): propagate network context
This commit is contained in:
@@ -35,12 +35,18 @@ function appendParams(url: URL, params: Record<string, unknown> = {}): void {
|
||||
}
|
||||
}
|
||||
|
||||
function withNetworkParam(params: Record<string, unknown> | undefined, network: string): Record<string, unknown> {
|
||||
if (params?.network !== undefined || params?.network_name !== undefined || params?.name !== undefined) return params ?? {};
|
||||
return { ...(params ?? {}), network };
|
||||
}
|
||||
|
||||
export async function requestJson(ctx: RuntimeContext, request: RequestOptions): Promise<[unknown, number]> {
|
||||
const { method, path, params, body, requireAuth = true, requireProject = false, requireNetworkCtx = false, requireUsernameCtx = false } = request;
|
||||
if (requireNetworkCtx) requireNetwork(ctx);
|
||||
const network = requireNetworkCtx ? requireNetwork(ctx) : null;
|
||||
if (requireUsernameCtx) requireUsername(ctx);
|
||||
const url = new URL(`/api/v1${path}`, ctx.server.replace(/\/+$/, ""));
|
||||
appendParams(url, params);
|
||||
const requestParams = network && (params !== undefined || body === undefined) ? withNetworkParam(params, network) : params;
|
||||
appendParams(url, requestParams);
|
||||
const started = performance.now();
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), ctx.timeout * 1000);
|
||||
|
||||
Reference in New Issue
Block a user