Browse Source

Refactor query code

Organize files.
master
TheoryOfNekomata 7 months ago
parent
commit
49d3791709
3 changed files with 19 additions and 5 deletions
  1. +4
    -0
      packages/core/src/common/queries/errors.ts
  2. +1
    -0
      packages/core/src/common/queries/index.ts
  3. +14
    -5
      packages/core/src/common/queries/media-types/application/x-www-form-urlencoded.ts

+ 4
- 0
packages/core/src/common/queries/errors.ts View File

@@ -0,0 +1,4 @@

export class DeserializeError extends Error {}

export class SerializeError extends Error {}

+ 1
- 0
packages/core/src/common/queries/index.ts View File

@@ -1,2 +1,3 @@
export * from './common';
export * from './errors';
export * as queryMediaTypes from './media-types';

+ 14
- 5
packages/core/src/common/queries/media-types/application/x-www-form-urlencoded.ts View File

@@ -5,6 +5,11 @@ import {
QueryOrGrouping,
} from '../../common';

import {
DeserializeError,
SerializeError,
} from '../../errors';

interface ProcessEntryBase {
type: string;
}
@@ -34,10 +39,12 @@ interface ProcessEntryBoolean extends ProcessEntryBase {
truthyStrings?: string[];
}

export type ProcessEntry = ProcessEntryString | ProcessEntryNumber | ProcessEntryBoolean;
type ProcessEntry = ProcessEntryString | ProcessEntryNumber | ProcessEntryBoolean;

export const name = 'application/x-www-form-urlencoded' as const;

class DeserializeInvalidFormatError extends DeserializeError {}

const normalizeRhs = (lhs: string, rhs: string, processEntriesMap?: Record<string, ProcessEntry>) => {
const defaultCoerceValues = {
type: 'string'
@@ -107,7 +114,7 @@ const normalizeRhs = (lhs: string, rhs: string, processEntriesMap?: Record<strin
}

const unknownCoerceValues = coerceValues as unknown as Record<string, string>;
throw new Error(`Invalid coercion type: ${unknownCoerceValues.type}`);
throw new DeserializeInvalidFormatError(`Invalid coercion type: ${unknownCoerceValues.type}`);
// this will be sent to the data source, e.g., the SQL query
// we can also make this function act as a "sanitizer"
}
@@ -186,6 +193,8 @@ export const deserialize: QueryMediaType<
)
};

class SerializeInvalidExpressionError extends SerializeError {}

const serializeExpression = (ex2: QueryAnyExpression) => {
if ('name' in ex2) {
return [ex2.name, `(${ex2.args.map((s) => s.toString()).join(',')})`];
@@ -193,7 +202,7 @@ const serializeExpression = (ex2: QueryAnyExpression) => {

if (ex2.rhs instanceof RegExp) {
if (ex2.operator !== 'REGEXP') {
throw new Error(`Invalid rhs given for operator: ${ex2.lhs} ${ex2.operator} <rhs>`);
throw new SerializeInvalidExpressionError(`Invalid rhs given for operator: ${ex2.lhs} ${ex2.operator} <rhs>`);
}

return [ex2.lhs, ex2.rhs.toString()];
@@ -210,7 +219,7 @@ const serializeExpression = (ex2: QueryAnyExpression) => {
default:
break;
}
throw new Error(`Invalid operator given for lhs: ${ex2.lhs} <op> ${ex2.rhs}`);
throw new SerializeInvalidExpressionError(`Invalid operator given for lhs: ${ex2.lhs} <op> ${ex2.rhs}`);
}
case 'number': {
return [ex2.lhs, ex2.rhs.toString()];
@@ -222,7 +231,7 @@ const serializeExpression = (ex2: QueryAnyExpression) => {
break;
}

throw new Error(`Unknown type for rhs: ${ex2.lhs} ${ex2.operator} <rhs>`);
throw new SerializeInvalidExpressionError(`Unknown type for rhs: ${ex2.lhs} ${ex2.operator} <rhs>`);
};

export const serialize: QueryMediaType<


Loading…
Cancel
Save