refactor UpdateOperation type

This commit is contained in:
DrakeTDL 2023-10-10 15:41:42 -07:00
parent 387a42f259
commit 21e3060c68
No known key found for this signature in database
2 changed files with 30 additions and 29 deletions

View File

@ -47,7 +47,7 @@ import {
StaffStaffMediaArgs,
StudioMediaArgs,
} from "./types/generated/graphql.ts"
import { Fetch, Fields, Fn, UpdateQuery } from "./types/Anilist.ts"
import { Fetch, Fields, Fn, UpdateOperation } from "./types/Anilist.ts"
import type { AtLeastOne } from "./types/AtLeastOne.ts"
const rewriteVarValues = (
@ -73,45 +73,37 @@ const replaceLast = (str: string, pattern: string | RegExp, replacement: string)
return last !== -1 ? `${str.slice(0, last)}${replacement}${str.slice(last + match.length)}` : str
}
const updateOperation = (
{ query, variables, field, level, hasSubField }: UpdateQuery,
const updateOperation: UpdateOperation = (
{ query, variables, fields, level, hasSubField },
) => {
if (!query) query = `${field[level]} {\n\t%${field[level]}\n}`
if (!query) query = `${fields[level]} {\n\t%${fields[level]}\n}`
const convertedType: [string[], string[]] = [[], []]
if (variables) {
const variable = Object.entries(variables)
for (let i = 0; i <= variable.length - 1; i++) {
convertedType[0].push(
`${variable[i][0]}: ${rewriteVarValues(variable[i][1])}`,
)
convertedType[0].push(`${variable[i][0]}: ${rewriteVarValues(variable[i][1])}`)
}
}
query = replaceLast(
query,
`%${field[level - 1]}`,
`${field[level]}${variables ? ` (${convertedType[0].join(", ")})` : ""}${
hasSubField ? `${` {\n${t(level + 1)}%${field.at(-1)}\n${t(level)}}`}` : ""
}\n${t(level)}%${field[level - 1]}`,
`%${fields[level - 1]}`,
`${fields[level]}${variables ? ` (${convertedType[0].join(", ")})` : ""}${
hasSubField ? `${` {\n${t(level + 1)}%${fields.at(-1)}\n${t(level)}}`}` : ""
}\n${t(level)}%${fields[level - 1]}`,
)
return {
get() {
// return query
return query!.replace(/(\n|,)?(\t+| )%\w+\b/g, "")
},
set(
{ subField, variables, hasSubField, level }:
& Pick<UpdateQuery, "hasSubField" | "variables">
& { subField: string; level: number },
) {
// const f = [...field]
field.splice(level, 0, subField)
while (field.length - 1 > level) field.pop()
set({ subField, variables, hasSubField, level }) {
fields.splice(level, 0, subField)
while (fields.length - 1 > level) fields.pop()
return updateOperation({ query, variables, level, field, hasSubField })
return updateOperation({ query, variables, level, fields, hasSubField })
},
}
}
const fetch: Fetch = async (init) => {
let host:
| "https://graphql.anilist.co"
@ -2772,7 +2764,7 @@ export const Client = function (auth?: { token: string }) {
},
get query() {
operation = updateOperation({
field: ["query"],
fields: ["query"],
level: 0,
})
@ -3227,7 +3219,7 @@ export const Client = function (auth?: { token: string }) {
},
get mutation() {
operation = updateOperation({
field: ["mutation"],
fields: ["mutation"],
level: 0,
})

View File

@ -71,12 +71,21 @@ export enum ScoreFormat {
export type Variables = { [arg: string]: string | number | boolean | string[] }
export type UpdateQuery = {
query?: string
variables?: any
field: string[]
level: number
hasSubField?: true
export type UpdateOperation = {
(opts: {
query?: string
variables?: any
fields: string[]
level: number
hasSubField?: true
}): {
get(): string
set(
{ subField, variables, hasSubField, level }:
& Omit<Parameters<UpdateOperation>[0], "query" | "fields">
& { subField: string },
): ReturnType<UpdateOperation>
}
}
export type Authorization = {