From 8bd7227db3bc09586b7299f99a9ecb0fe75564e0 Mon Sep 17 00:00:00 2001 From: Natoandro Date: Mon, 3 Feb 2025 13:04:30 +0300 Subject: [PATCH 1/5] graphql-compliant payload on failed requests --- .ghjk/deno.lock | 1 + src/typegate/src/errors.ts | 1 + src/typegate/src/services/artifact_service.ts | 61 +++++++------------ src/typegate/src/services/auth/mod.ts | 4 +- .../src/services/auth/protocols/oauth2.ts | 16 +++-- src/typegate/src/services/auth/routes/take.ts | 19 ++---- .../src/services/auth/routes/validate.ts | 17 +++--- src/typegate/src/services/responses.ts | 2 +- 8 files changed, 50 insertions(+), 71 deletions(-) diff --git a/.ghjk/deno.lock b/.ghjk/deno.lock index b9be4c0da..7ec8db2ca 100644 --- a/.ghjk/deno.lock +++ b/.ghjk/deno.lock @@ -608,6 +608,7 @@ "https://raw.githubusercontent.com/metatypedev/ghjk/v0.2.1/deps/common.ts": "f775710b66a9099b98651cd3831906466e9b83ef98f2e5c080fd59ee801c28d4", "https://raw.githubusercontent.com/metatypedev/ghjk/v0.2.1/deps/ports.ts": "3c60d1f7ab626ffdd81b37f4e83a780910936480da8fe24f4ccceaefa207d339", "https://raw.githubusercontent.com/metatypedev/ghjk/v0.2.1/files/deno/mod.ts": "1b8204c3df18b908408b2148b48af788e669d0debbeb8ba119418ab1ddf1ab8f", + "https://raw.githubusercontent.com/metatypedev/ghjk/v0.2.1/files/deno/worker.ts": "8ded400d70a0bd40e281ceb1ffcdc82578443caf9c481b9eee77166472784282", "https://raw.githubusercontent.com/metatypedev/ghjk/v0.2.1/files/mod.ts": "44a8874c6ee9f086b7a521d4956c1802be201d01f9e91329d52a4b96738f7a34", "https://raw.githubusercontent.com/metatypedev/ghjk/v0.2.1/host/mod.ts": "af5a9704c3a5b410b322afe0bc8caaaac5b28e1e1591d82b0c5fb53f92cbc97f", "https://raw.githubusercontent.com/metatypedev/ghjk/v0.2.1/host/types.ts": "f450d9b9c0eced2650262d02455aa6f794de0edd6b052aade256882148e5697f", diff --git a/src/typegate/src/errors.ts b/src/typegate/src/errors.ts index 16f51f687..65ccc80b9 100644 --- a/src/typegate/src/errors.ts +++ b/src/typegate/src/errors.ts @@ -4,6 +4,7 @@ import { basename, dirname, extname } from "@std/path"; import { getLogger } from "./log.ts"; import { globalConfig } from "./config.ts"; +import { jsonError } from "./services/responses.ts"; const logger = getLogger(import.meta); diff --git a/src/typegate/src/services/artifact_service.ts b/src/typegate/src/services/artifact_service.ts index 1a9489a65..2348f51c7 100644 --- a/src/typegate/src/services/artifact_service.ts +++ b/src/typegate/src/services/artifact_service.ts @@ -9,6 +9,8 @@ import { import { z } from "zod"; import { getLogger } from "../log.ts"; import { BaseError, UnknownError } from "../errors.ts"; +import { jsonError } from "./responses.ts"; +import { jsonOk } from "./responses.ts"; const logger = getLogger(import.meta); @@ -25,10 +27,11 @@ export class ArtifactService { if (operation === "prepare-upload") { if (request.method !== "POST") { logger.warn("Method not allowed: {}", request.method); - return new Response(JSON.stringify({ error: "method not allowed" }), { - status: 405, - headers: { "Content-Type": "application/json" }, - }); + return jsonError( + `method not allowed: ${request.method}`, + new Headers(), + 405, + ); } let metaList: Array; @@ -36,20 +39,16 @@ export class ArtifactService { metaList = prepareUploadBodySchema.parse(await request.json()); } catch (error) { logger.error("Failed to parse data: {}", error); - return new Response( - JSON.stringify({ error: `Invalid Request Body: ${error.message}` }), - { - status: 400, - headers: { "Content-Type": "application/json" }, - }, + return jsonError( + `invalid request body: ${error.message}`, + new Headers(), + 400, ); } try { const data = await this.#createUploadTokens(metaList, tgName); - return new Response(JSON.stringify(data), { - headers: { "Content-Type": "application/json" }, - }); + return jsonOk(data, new Headers()); } catch (e) { if (e instanceof BaseError) { return e.toResponse(); @@ -60,28 +59,23 @@ export class ArtifactService { if (operation) { logger.warn("not found: {} {}", request.method, url.toString()); - return new Response(JSON.stringify({ message: "not found" }), { - status: 404, - headers: { "Content-Type": "application/json" }, - }); + return jsonError("not found", new Headers(), 404); } if (request.method !== "POST") { logger.warn("Method not allowed: {}", request.method); - return new Response(JSON.stringify({ error: "method not allowed" }), { - status: 405, - headers: { "Content-Type": "application/json" }, - }); + return jsonError( + `method not allowed: ${request.method}`, + new Headers(), + 405, + ); } const token = url.searchParams.get("token"); if (!token) { logger.warn("Missing upload token"); - return new Response(JSON.stringify({ error: "missing token" }), { - status: 403, - headers: { "Content-Type": "application/json" }, - }); + return jsonError("missing token", new Headers(), 403); } return await this.#handleUpload(token, request.body!, tgName); @@ -110,10 +104,7 @@ export class ArtifactService { if (e instanceof BaseError) { return e.toResponse(); } - return new Response(JSON.stringify({ error: e.message }), { - status: 500, - headers: { "Content-Type": "application/json" }, - }); + return jsonError(e.message, new Headers(), 500); } if (meta.typegraphName !== tgName) { @@ -125,17 +116,9 @@ export class ArtifactService { if (hash !== meta.hash) { await this.store.persistence.delete(hash); logger.warn("hash mismatch: {} {}", hash, meta.hash); - return new Response(JSON.stringify({ error: "hash mismatch" }), { - status: 403, - headers: { "Content-Type": "application/json" }, - }); + return jsonError("hash mismatch", new Headers(), 403); } - return new Response(JSON.stringify({ status: "ok" }), { - status: 201, - headers: { - "Content-Type": "application/json", - }, - }); + return jsonOk({ status: "ok" }, new Headers(), 201); } } diff --git a/src/typegate/src/services/auth/mod.ts b/src/typegate/src/services/auth/mod.ts index 16175e4ea..93d4aa008 100644 --- a/src/typegate/src/services/auth/mod.ts +++ b/src/typegate/src/services/auth/mod.ts @@ -132,9 +132,7 @@ export async function handleAuth( uri: `${url.protocol}//${url.host}/${engine.name}/auth/${name}?redirect_uri=${origin}`, })); - return new Response(JSON.stringify({ providers }), { - headers: { "content-type": "application/json" }, - }); + return jsonOk({ providers }, new Headers()); } return await provider.authMiddleware(request); diff --git a/src/typegate/src/services/auth/protocols/oauth2.ts b/src/typegate/src/services/auth/protocols/oauth2.ts index 09729b440..0b34177cd 100644 --- a/src/typegate/src/services/auth/protocols/oauth2.ts +++ b/src/typegate/src/services/auth/protocols/oauth2.ts @@ -26,6 +26,7 @@ import { generateWeakValidator, } from "../../../engine/typecheck/input.ts"; import type { TokenMiddlewareOutput } from "./protocol.ts"; +import { jsonError } from "../../responses.ts"; const logger = getLogger(import.meta); @@ -205,10 +206,11 @@ export class OAuth2Auth extends Protocol { new Headers(), ); // https://github.com/cmd-johnson/deno-oauth2-client/issues/25 - return new Response(`invalid oauth2, check your credentials: ${e}`, { - status: 400, + return jsonError( + `invalid oauth2, check your credentials: ${e}`, headers, - }); + 400, + ); } } @@ -237,9 +239,11 @@ export class OAuth2Auth extends Protocol { }); } - return new Response("missing origin or redirect_uri query parameter", { - status: 400, - }); + return jsonError( + "missing origin or redirect_uri query parameter", + new Headers(), + 400, + ); } async tokenMiddleware( diff --git a/src/typegate/src/services/auth/routes/take.ts b/src/typegate/src/services/auth/routes/take.ts index 53d884568..d19e0563c 100644 --- a/src/typegate/src/services/auth/routes/take.ts +++ b/src/typegate/src/services/auth/routes/take.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: MPL-2.0 import { getLogger } from "../../../log.ts"; +import { jsonError, jsonOk } from "../../responses.ts"; import { clearCookie, getEncryptedCookie } from "../cookies.ts"; import type { RouteParams } from "./mod.ts"; @@ -20,24 +21,16 @@ export async function take(params: RouteParams) { ); if (!redirectUri.startsWith(origin)) { - return new Response( + return jsonError( "take request must share domain with redirect uri", - { - status: 400, - headers: resHeaders, - }, + resHeaders, + 400, ); } resHeaders.set("content-type", "application/json"); - return new Response(JSON.stringify({ token }), { - status: 200, - headers: resHeaders, - }); + return jsonOk({ token }, resHeaders); } catch (e) { logger.info(`take request failed ${e}`); - return new Response("not authorized", { - status: 401, - headers: resHeaders, - }); + return jsonError("not authorized", resHeaders, 401); } } diff --git a/src/typegate/src/services/auth/routes/validate.ts b/src/typegate/src/services/auth/routes/validate.ts index 7e97d9172..acfacdafc 100644 --- a/src/typegate/src/services/auth/routes/validate.ts +++ b/src/typegate/src/services/auth/routes/validate.ts @@ -1,13 +1,12 @@ // Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. // SPDX-License-Identifier: MPL-2.0 +import { jsonError, jsonOk } from "../../responses.ts"; import type { RouteParams } from "./mod.ts"; +// TODO use `BaseError` export function badRequest(message: string): Response { - return new Response(message, { - status: 400, - headers: { "content-type": "text/plain" }, - }); + return jsonError(message, new Headers(), 400); } type Action = @@ -69,11 +68,11 @@ export async function validate(params: RouteParams): Promise { } // return json response - return new Response(JSON.stringify(result), { - headers: { - "content-type": "application/json", + return jsonOk( + result, + new Headers({ "access-control-allow-origin": "*", ...headers, - }, - }); + }), + ); } diff --git a/src/typegate/src/services/responses.ts b/src/typegate/src/services/responses.ts index f188940bf..680440e72 100644 --- a/src/typegate/src/services/responses.ts +++ b/src/typegate/src/services/responses.ts @@ -4,7 +4,7 @@ import type { JSONValue } from "../utils.ts"; import { BaseError, ErrorKind } from "../errors.ts"; -export const jsonOk = (data: JSONValue, headers: Headers) => { +export const jsonOk = (data: JSONValue, headers: Headers, code = 200) => { headers.set("content-type", "application/json"); return new Response(JSON.stringify({ data }), { status: 200, From f6c6293f457669a5692e244d3b0850276c249fd8 Mon Sep 17 00:00:00 2001 From: Natoandro Date: Tue, 4 Feb 2025 10:14:11 +0300 Subject: [PATCH 2/5] change function signature --- src/typegate/src/errors.ts | 1 - src/typegate/src/services/artifact_service.ts | 37 +++++++++---------- .../src/services/auth/protocols/oauth2.ts | 17 ++++----- src/typegate/src/services/auth/routes/take.ts | 18 +++++---- .../src/services/auth/routes/validate.ts | 10 ++--- src/typegate/src/services/graphql_service.ts | 14 +++---- src/typegate/src/services/responses.ts | 28 +++++++++++--- src/typegate/src/typegate/mod.ts | 2 +- 8 files changed, 72 insertions(+), 55 deletions(-) diff --git a/src/typegate/src/errors.ts b/src/typegate/src/errors.ts index 65ccc80b9..16f51f687 100644 --- a/src/typegate/src/errors.ts +++ b/src/typegate/src/errors.ts @@ -4,7 +4,6 @@ import { basename, dirname, extname } from "@std/path"; import { getLogger } from "./log.ts"; import { globalConfig } from "./config.ts"; -import { jsonError } from "./services/responses.ts"; const logger = getLogger(import.meta); diff --git a/src/typegate/src/services/artifact_service.ts b/src/typegate/src/services/artifact_service.ts index 2348f51c7..dbdeb9d03 100644 --- a/src/typegate/src/services/artifact_service.ts +++ b/src/typegate/src/services/artifact_service.ts @@ -27,11 +27,10 @@ export class ArtifactService { if (operation === "prepare-upload") { if (request.method !== "POST") { logger.warn("Method not allowed: {}", request.method); - return jsonError( - `method not allowed: ${request.method}`, - new Headers(), - 405, - ); + return jsonError({ + message: `method not allowed: ${request.method}`, + status: 405, + }); } let metaList: Array; @@ -39,11 +38,10 @@ export class ArtifactService { metaList = prepareUploadBodySchema.parse(await request.json()); } catch (error) { logger.error("Failed to parse data: {}", error); - return jsonError( - `invalid request body: ${error.message}`, - new Headers(), - 400, - ); + return jsonError({ + message: `invalid request body: ${error.message}`, + status: 400, + }); } try { @@ -59,23 +57,22 @@ export class ArtifactService { if (operation) { logger.warn("not found: {} {}", request.method, url.toString()); - return jsonError("not found", new Headers(), 404); + return jsonError({ message: "not found", status: 404 }); } if (request.method !== "POST") { logger.warn("Method not allowed: {}", request.method); - return jsonError( - `method not allowed: ${request.method}`, - new Headers(), - 405, - ); + return jsonError({ + message: `method not allowed: ${request.method}`, + status: 405, + }); } const token = url.searchParams.get("token"); if (!token) { logger.warn("Missing upload token"); - return jsonError("missing token", new Headers(), 403); + return jsonError({ message: "missing token", status: 403 }); } return await this.#handleUpload(token, request.body!, tgName); @@ -104,7 +101,7 @@ export class ArtifactService { if (e instanceof BaseError) { return e.toResponse(); } - return jsonError(e.message, new Headers(), 500); + return jsonError({ message: e.message, status: 500 }); } if (meta.typegraphName !== tgName) { @@ -116,9 +113,9 @@ export class ArtifactService { if (hash !== meta.hash) { await this.store.persistence.delete(hash); logger.warn("hash mismatch: {} {}", hash, meta.hash); - return jsonError("hash mismatch", new Headers(), 403); + return jsonError({ message: "hash mismatch", status: 403 }); } - return jsonOk({ status: "ok" }, new Headers(), 201); + return jsonOk({ data: { status: "ok" }, status: 201 }); } } diff --git a/src/typegate/src/services/auth/protocols/oauth2.ts b/src/typegate/src/services/auth/protocols/oauth2.ts index 0b34177cd..ea0f03803 100644 --- a/src/typegate/src/services/auth/protocols/oauth2.ts +++ b/src/typegate/src/services/auth/protocols/oauth2.ts @@ -206,11 +206,11 @@ export class OAuth2Auth extends Protocol { new Headers(), ); // https://github.com/cmd-johnson/deno-oauth2-client/issues/25 - return jsonError( - `invalid oauth2, check your credentials: ${e}`, + return jsonError({ + message: `invalid oauth2, check your credentials: ${e}`, headers, - 400, - ); + status: 400, + }); } } @@ -239,11 +239,10 @@ export class OAuth2Auth extends Protocol { }); } - return jsonError( - "missing origin or redirect_uri query parameter", - new Headers(), - 400, - ); + return jsonError({ + message: "missing origin or redirect_uri query parameter", + status: 400, + }); } async tokenMiddleware( diff --git a/src/typegate/src/services/auth/routes/take.ts b/src/typegate/src/services/auth/routes/take.ts index d19e0563c..6091246e4 100644 --- a/src/typegate/src/services/auth/routes/take.ts +++ b/src/typegate/src/services/auth/routes/take.ts @@ -21,16 +21,20 @@ export async function take(params: RouteParams) { ); if (!redirectUri.startsWith(origin)) { - return jsonError( - "take request must share domain with redirect uri", - resHeaders, - 400, - ); + return jsonError({ + status: 400, + message: "take request must share domain with redirect uri", + headers: resHeaders, + }); } resHeaders.set("content-type", "application/json"); - return jsonOk({ token }, resHeaders); + return jsonOk({ data: { token }, headers: resHeaders }); } catch (e) { logger.info(`take request failed ${e}`); - return jsonError("not authorized", resHeaders, 401); + return jsonError({ + status: 401, + message: "not authorized", + headers: resHeaders, + }); } } diff --git a/src/typegate/src/services/auth/routes/validate.ts b/src/typegate/src/services/auth/routes/validate.ts index acfacdafc..5649e7a39 100644 --- a/src/typegate/src/services/auth/routes/validate.ts +++ b/src/typegate/src/services/auth/routes/validate.ts @@ -6,7 +6,7 @@ import type { RouteParams } from "./mod.ts"; // TODO use `BaseError` export function badRequest(message: string): Response { - return jsonError(message, new Headers(), 400); + return jsonError({ status: 400, message }); } type Action = @@ -68,11 +68,11 @@ export async function validate(params: RouteParams): Promise { } // return json response - return jsonOk( - result, - new Headers({ + return jsonOk({ + data: result, + headers: new Headers({ "access-control-allow-origin": "*", ...headers, }), - ); + }); } diff --git a/src/typegate/src/services/graphql_service.ts b/src/typegate/src/services/graphql_service.ts index 65be78f48..6ca1828cf 100644 --- a/src/typegate/src/services/graphql_service.ts +++ b/src/typegate/src/services/graphql_service.ts @@ -119,7 +119,7 @@ export async function handleGraphQL( ); } - return jsonOk(res, headers); + return jsonOk({ data: res, headers }); } catch (e) { // throw e; if (e instanceof BaseError) { @@ -127,14 +127,14 @@ export async function handleGraphQL( } if (e instanceof ResolverError) { logger.error(`field err: ${e.message}`); - return jsonError(e.message, headers, 502); + return jsonError({ status: 502, message: e.message, headers }); } else if (e instanceof BadContext) { logger.error(`context err: ${e.message}`); - return jsonError( - e.message, + return jsonError({ + status: Object.keys(context).length === 0 ? 401 : 403, + message: e.message, headers, - Object.keys(context).length === 0 ? 401 : 403, - ); + }); } else { logger.error(`request err: ${Deno.inspect(e)}`); if (e.cause) { @@ -142,7 +142,7 @@ export async function handleGraphQL( Deno.inspect(e.cause, { strAbbreviateSize: 1024, depth: 10 }), ); } - return jsonError(e.message, headers, 400); + jsonError({ status: 400, message: e.message, headers }); } } } diff --git a/src/typegate/src/services/responses.ts b/src/typegate/src/services/responses.ts index 680440e72..40e047fe7 100644 --- a/src/typegate/src/services/responses.ts +++ b/src/typegate/src/services/responses.ts @@ -4,19 +4,37 @@ import type { JSONValue } from "../utils.ts"; import { BaseError, ErrorKind } from "../errors.ts"; -export const jsonOk = (data: JSONValue, headers: Headers, code = 200) => { +export type JsonOkConfig = { + data: JSONValue; + headers?: Headers | HeadersInit; + status?: number; +}; + +export const jsonOk = ( + { status = 200, data, headers: headersInit }: JsonOkConfig, +) => { + const headers = headersInit != null + ? new Headers(headersInit) + : new Headers(); headers.set("content-type", "application/json"); return new Response(JSON.stringify({ data }), { - status: 200, + status, headers, }); }; +export type JsonErrorConfig = { + status: number; + message: string; + headers?: Headers | HeadersInit; +}; + export const jsonError = ( - message: string, - headers: Headers, - status: number, + { status, message, headers: headersInit }: JsonErrorConfig, ) => { + const headers = headersInit != null + ? new Headers(headersInit) + : new Headers(); headers.set("content-type", "application/json"); return new Response( JSON.stringify({ diff --git a/src/typegate/src/typegate/mod.ts b/src/typegate/src/typegate/mod.ts index 815699ef7..d090fca21 100644 --- a/src/typegate/src/typegate/mod.ts +++ b/src/typegate/src/typegate/mod.ts @@ -285,7 +285,7 @@ export class Typegate implements AsyncDisposable { new Headers(cors), ).catch((e) => e); if (jwtCheck instanceof Error) { - return jsonError(jwtCheck.message, new Headers(), 401); + return jsonError({ message: jwtCheck.message, status: 401 }); } const [context, headers] = jwtCheck; From 19f44ce86c547144d9359f314b12efdfc0055e5e Mon Sep 17 00:00:00 2001 From: Natoandro Date: Tue, 4 Feb 2025 10:17:13 +0300 Subject: [PATCH 3/5] remove unnecessary comment --- src/typegate/src/services/auth/routes/validate.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/typegate/src/services/auth/routes/validate.ts b/src/typegate/src/services/auth/routes/validate.ts index 5649e7a39..5394ad778 100644 --- a/src/typegate/src/services/auth/routes/validate.ts +++ b/src/typegate/src/services/auth/routes/validate.ts @@ -4,7 +4,6 @@ import { jsonError, jsonOk } from "../../responses.ts"; import type { RouteParams } from "./mod.ts"; -// TODO use `BaseError` export function badRequest(message: string): Response { return jsonError({ status: 400, message }); } From 3bebdd85ee89039c65f0b5d61a42455824b221cb Mon Sep 17 00:00:00 2001 From: Natoandro Date: Tue, 4 Feb 2025 15:32:41 +0300 Subject: [PATCH 4/5] fix --- src/typegate/src/services/artifact_service.ts | 2 +- src/typegate/src/services/auth/mod.ts | 4 ++-- src/typegate/src/services/graphql_service.ts | 2 +- src/typegate/src/services/responses.ts | 6 ++++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/typegate/src/services/artifact_service.ts b/src/typegate/src/services/artifact_service.ts index dbdeb9d03..9a6b438e5 100644 --- a/src/typegate/src/services/artifact_service.ts +++ b/src/typegate/src/services/artifact_service.ts @@ -46,7 +46,7 @@ export class ArtifactService { try { const data = await this.#createUploadTokens(metaList, tgName); - return jsonOk(data, new Headers()); + return jsonOk({ data, graphql: false }); } catch (e) { if (e instanceof BaseError) { return e.toResponse(); diff --git a/src/typegate/src/services/auth/mod.ts b/src/typegate/src/services/auth/mod.ts index 93d4aa008..e87acf4f2 100644 --- a/src/typegate/src/services/auth/mod.ts +++ b/src/typegate/src/services/auth/mod.ts @@ -14,7 +14,7 @@ import { unsafeExtractJWT } from "../../crypto.ts"; import type { QueryEngine } from "../../engine/query_engine.ts"; import * as routes from "./routes/mod.ts"; import { getLogger } from "../../log.ts"; -import { methodNotAllowed } from "../../services/responses.ts"; +import { jsonOk, methodNotAllowed } from "../../services/responses.ts"; import type { Runtime } from "../../runtimes/Runtime.ts"; const logger = getLogger(import.meta); @@ -132,7 +132,7 @@ export async function handleAuth( uri: `${url.protocol}//${url.host}/${engine.name}/auth/${name}?redirect_uri=${origin}`, })); - return jsonOk({ providers }, new Headers()); + return jsonOk({ data: providers }); } return await provider.authMiddleware(request); diff --git a/src/typegate/src/services/graphql_service.ts b/src/typegate/src/services/graphql_service.ts index 6ca1828cf..1546fca86 100644 --- a/src/typegate/src/services/graphql_service.ts +++ b/src/typegate/src/services/graphql_service.ts @@ -142,7 +142,7 @@ export async function handleGraphQL( Deno.inspect(e.cause, { strAbbreviateSize: 1024, depth: 10 }), ); } - jsonError({ status: 400, message: e.message, headers }); + return jsonError({ status: 400, message: e.message, headers }); } } } diff --git a/src/typegate/src/services/responses.ts b/src/typegate/src/services/responses.ts index 40e047fe7..a7244b11f 100644 --- a/src/typegate/src/services/responses.ts +++ b/src/typegate/src/services/responses.ts @@ -8,16 +8,18 @@ export type JsonOkConfig = { data: JSONValue; headers?: Headers | HeadersInit; status?: number; + graphql?: boolean; }; export const jsonOk = ( - { status = 200, data, headers: headersInit }: JsonOkConfig, + { status = 200, data, headers: headersInit, graphql = true }: JsonOkConfig, ) => { const headers = headersInit != null ? new Headers(headersInit) : new Headers(); headers.set("content-type", "application/json"); - return new Response(JSON.stringify({ data }), { + const payload = graphql ? { data } : data; + return new Response(JSON.stringify(payload), { status, headers, }); From e2fe9355194da128b34bf159412d9d40c657cb93 Mon Sep 17 00:00:00 2001 From: Natoandro Date: Tue, 4 Feb 2025 18:56:00 +0300 Subject: [PATCH 5/5] fix #2 --- src/typegate/src/services/artifact_service.ts | 2 +- src/typegate/src/services/graphql_service.ts | 2 +- src/typegate/src/services/responses.ts | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/typegate/src/services/artifact_service.ts b/src/typegate/src/services/artifact_service.ts index 9a6b438e5..a05e4426e 100644 --- a/src/typegate/src/services/artifact_service.ts +++ b/src/typegate/src/services/artifact_service.ts @@ -46,7 +46,7 @@ export class ArtifactService { try { const data = await this.#createUploadTokens(metaList, tgName); - return jsonOk({ data, graphql: false }); + return jsonOk({ data }); } catch (e) { if (e instanceof BaseError) { return e.toResponse(); diff --git a/src/typegate/src/services/graphql_service.ts b/src/typegate/src/services/graphql_service.ts index 1546fca86..a9278b744 100644 --- a/src/typegate/src/services/graphql_service.ts +++ b/src/typegate/src/services/graphql_service.ts @@ -119,7 +119,7 @@ export async function handleGraphQL( ); } - return jsonOk({ data: res, headers }); + return jsonOk({ data: { data: res }, headers }); } catch (e) { // throw e; if (e instanceof BaseError) { diff --git a/src/typegate/src/services/responses.ts b/src/typegate/src/services/responses.ts index a7244b11f..3b0cb81bc 100644 --- a/src/typegate/src/services/responses.ts +++ b/src/typegate/src/services/responses.ts @@ -8,18 +8,16 @@ export type JsonOkConfig = { data: JSONValue; headers?: Headers | HeadersInit; status?: number; - graphql?: boolean; }; export const jsonOk = ( - { status = 200, data, headers: headersInit, graphql = true }: JsonOkConfig, + { status = 200, data, headers: headersInit }: JsonOkConfig, ) => { const headers = headersInit != null ? new Headers(headersInit) : new Headers(); headers.set("content-type", "application/json"); - const payload = graphql ? { data } : data; - return new Response(JSON.stringify(payload), { + return new Response(JSON.stringify(data), { status, headers, });