Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.7 KB
Raw Blame
// Push data has no HTTP endpoint, so Orval only emits its TypeScript type.
// Generate runtime validators from the same Rust-owned OpenAPI components.
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { jsonSchemaToZod } from 'json-schema-to-zod';
import { z } from 'zod';

const serviceDir = new URL(
  '../src/lib/service-clients/service-notification/',
  import.meta.url
);
const resolved = await $RefParser.dereference(
  await Bun.file(new URL('openapi.json', serviceDir)).json()
);
const { components } = z
  .object({
    components: z.object({
      schemas: z.record(z.string(), z.record(z.string(), z.unknown())),
    }),
  })
  .parse(resolved);

const validators = [
  ['PushNotificationNavigation', 'pushNotificationNavigation'],
  ['PushNotificationData', 'pushNotificationData'],
].map(([component, name]) => {
  const schema = components.schemas[component];
  if (!schema) throw new Error(`Missing push schema: ${component}`);
  return jsonSchemaToZod(schema, {
    module: 'esm',
    name,
    withoutDescribes: true,
    noImport: true,
    // Rust's Option<enum> is a oneOf(null, enum). Those branches are disjoint;
    // use a typed union instead of the converter's Zod 3-only oneOf refinement.
    parserOverride: (node) => {
      if (
        node.oneOf?.some(
          (branch) => typeof branch === 'object' && branch.type === 'null'
        )
      ) {
        return jsonSchemaToZod(
          { ...node, oneOf: undefined, anyOf: node.oneOf },
          { withoutDescribes: true }
        );
      }
    },
  });
});

await Bun.write(
  new URL('generated/push.ts', serviceDir),
  `/* Generated by scripts/generate-push-schema.ts. Do not edit manually. */
import { z } from 'zod';

${validators.join('\n\n')}
`
);