feat: add payment module
This commit is contained in:
1
platform/parse/mod.ts
Normal file
1
platform/parse/mod.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./zod.ts";
|
||||
13
platform/parse/package.json
Normal file
13
platform/parse/package.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "@platform/parse",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./mod.ts",
|
||||
"exports": {
|
||||
".": "./mod.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"zod": "4.1.13"
|
||||
}
|
||||
}
|
||||
13
platform/parse/time.ts
Normal file
13
platform/parse/time.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import z from "zod";
|
||||
|
||||
export const ZonedDateTimeSchema = z.object({
|
||||
tag: z.literal("time/zoned-date-time"),
|
||||
rep: z.string(),
|
||||
hashCode: z.number(),
|
||||
});
|
||||
|
||||
export function assertZonedDateTime(value: object | null): value is ZonedDateTime {
|
||||
return ZonedDateTimeSchema.parse(value) !== undefined;
|
||||
}
|
||||
|
||||
export type ZonedDateTime = z.output<typeof ZonedDateTimeSchema>;
|
||||
66
platform/parse/zod.ts
Normal file
66
platform/parse/zod.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import z, { type ZodType } from "zod";
|
||||
|
||||
import { assertZonedDateTime } from "./time.ts";
|
||||
|
||||
/**
|
||||
* Takes a single record from a list of database rows.
|
||||
*
|
||||
* @param rows - List of rows to retrieve record from.
|
||||
*/
|
||||
export function takeOne<TSchema extends ZodType>(
|
||||
schema: TSchema,
|
||||
): (records: unknown[]) => z.output<TSchema> | undefined {
|
||||
return (records: unknown[]) => {
|
||||
if (records[0] === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return schema.parse(records[0]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes all records from a list of database rows and validates each one.
|
||||
*
|
||||
* @param schema - Zod schema to validate each record against.
|
||||
*/
|
||||
export function takeAll<TSchema extends ZodType>(schema: TSchema): (records: unknown[]) => z.output<TSchema>[] {
|
||||
return (records: unknown[]) => {
|
||||
return records.map((record) => schema.parse(record));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a XTDB NEST_MANY sub query to the provided schema.
|
||||
*
|
||||
* @param schema - Schema to parse the NEST_MANY result against.
|
||||
*/
|
||||
export function nestMany<TSchema extends ZodType>(schema: TSchema) {
|
||||
return z.array(z.preprocess(xtdbEntriesToObject, schema)).default([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for a _entries key on the provided record and re-map them to
|
||||
* a new object if _entries exists.
|
||||
*
|
||||
* @param record - Record to re-map.
|
||||
*/
|
||||
function xtdbEntriesToObject(record: any) {
|
||||
if ("_entries" in record) {
|
||||
const obj: any = {};
|
||||
for (let i = 0; i < record._entries.length; i += 2) {
|
||||
obj[record._entries[i]] = xtdbEntryValue(record._entries[i + 1]);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
function xtdbEntryValue(value: unknown) {
|
||||
if (Array.isArray(value) === true) {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "object" && assertZonedDateTime(value) === true) {
|
||||
return value.rep.replace("[UTC]", "");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user