Skip to content

Commit

Permalink
feat(parses.ts): add parseGraphQLParameters that parse value as Gra…
Browse files Browse the repository at this point in the history
…phQL parameters
  • Loading branch information
TomokiMiyauci committed Aug 4, 2022
1 parent 3675fc3 commit be9128b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
17 changes: 13 additions & 4 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<T, K = keyof T> =
Omit<T, K & keyof T> & Partial<Pick<T, K & keyof T>> extends infer U
Expand Down Expand Up @@ -84,3 +85,11 @@ export async function tryCatch<T>(
return [, er];
}
}

// deno-lint-ignore no-explicit-any
export function has<T extends Record<any, any>, K extends string>(
value: T,
key: K,
): value is T & Record<K, unknown> {
return key in value;
}
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
78 changes: 78 additions & 0 deletions parses.ts
Original file line number Diff line number Diff line change
@@ -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,
}];
}

0 comments on commit be9128b

Please sign in to comment.