From be9128be4f68fd3a02298eb090eda94779a29445 Mon Sep 17 00:00:00 2001 From: TomokiMiyauci Date: Thu, 4 Aug 2022 12:28:08 +0900 Subject: [PATCH] feat(parses.ts): add `parseGraphQLParameters` that parse value as GraphQL parameters --- deps.ts | 17 +++++++++--- mod.ts | 2 ++ parses.ts | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 parses.ts diff --git a/deps.ts b/deps.ts index 794734d..417b553 100644 --- a/deps.ts +++ b/deps.ts @@ -19,15 +19,16 @@ export { export { contentType, parseMediaType, -} from "https://deno.land/std@0.148.0/media_types/mod.ts"; -export { accepts } from "https://deno.land/std@0.148.0/http/negotiation.ts"; +} from "https://deno.land/std@0.150.0/media_types/mod.ts"; +export { accepts } from "https://deno.land/std@0.150.0/http/negotiation.ts"; export { isNil, isNull, isObject, + isPlainObject, isString, isUndefined, -} from "https://deno.land/x/isx@v1.0.0-beta.17/mod.ts"; +} from "https://deno.land/x/isx@1.0.0-beta.19/mod.ts"; export { JSON, type json, @@ -42,7 +43,7 @@ export { createHttpError, HttpError, Status, -} from "https://deno.land/std@0.148.0/http/mod.ts"; +} from "https://deno.land/std@0.150.0/http/mod.ts"; export type PartialBy = Omit & Partial> extends infer U @@ -84,3 +85,11 @@ export async function tryCatch( return [, er]; } } + +// deno-lint-ignore no-explicit-any +export function has, K extends string>( + value: T, + key: K, +): value is T & Record { + return key in value; +} diff --git a/mod.ts b/mod.ts index 6c73503..ef97631 100644 --- a/mod.ts +++ b/mod.ts @@ -4,3 +4,5 @@ export { createRequest, resolveRequest } from "./requests.ts"; export { createResponse, resolveResponse } from "./responses.ts"; export { gql } from "./utils.ts"; export { default as usePlayground } from "./use/playground.ts"; +export { parseGraphQLParameters } from "./parses.ts"; +export { type GraphQLParameters } from "./types.ts"; diff --git a/parses.ts b/parses.ts new file mode 100644 index 0000000..1a38d27 --- /dev/null +++ b/parses.ts @@ -0,0 +1,78 @@ +import { has, isNull, isPlainObject, isString } from "./deps.ts"; +import { GraphQLParameters } from "./types.ts"; + +/** Parse value as {@link GraphQLParameters}. */ +export function parseGraphQLParameters( + value: unknown, +): [data: GraphQLParameters] | [data: undefined, error: TypeError] { + if (!isPlainObject(value)) { + return [ + , + TypeError( + `Invalid field. "payload" must be plain object.`, + ), + ]; + } + + if (!has(value, "query")) { + return [ + , + TypeError( + `Missing field. "query"`, + ), + ]; + } + + if (!isString(value.query)) { + return [ + , + TypeError( + `Invalid field. "query" must be string.`, + ), + ]; + } + + if ( + has(value, "variables") && + (!isNull(value.variables) && !isPlainObject(value.variables)) + ) { + return [ + , + TypeError( + `Invalid field. "variables" must be plain object or null`, + ), + ]; + } + if ( + has(value, "operationName") && + (!isNull(value.operationName) && !isString(value.operationName)) + ) { + return [ + , + TypeError( + `Invalid field. "operationName" must be string or null.`, + ), + ]; + } + if ( + has(value, "extensions") && + (!isNull(value.extensions) && !isPlainObject(value.extensions)) + ) { + return [ + , + TypeError( + `Invalid field. "extensions" must be plain object or null`, + ), + ]; + } + + const { query, ...rest } = value; + + return [{ + operationName: null, + variableValues: null, + extensions: null, + query, + ...rest, + }]; +}