madapes

@aurora/codegen-eos (0.3.3)

Published 2025-12-27 10:44:50 +00:00 by vlad

Installation

@aurora:registry=
npm install @aurora/codegen-eos@0.3.3
"@aurora/codegen-eos": "0.3.3"

About this package

@aurora/codegen-eos

Code generation engine for Eos - generates typed hooks, types, and SDKs from Aurora schemas.

Overview

The codegen-eos package transforms Aurora schema definitions into Eos-compatible frontend code:

  • TypeScript types for all API routes
  • Client SDK matching Eos requirements
  • Typed query hooks using Eos's useQuery
  • Typed mutation hooks using Eos's useMutation
  • Typed subscription hooks using Eos's useSubscription
  • Route metadata for navigation helpers

Installation

bun add -D @aurora/codegen-eos

Usage

The package is automatically used by @aurora/codegen when the eos option is enabled:

aurora codegen --eos

Or programmatically:

import { codegen } from "@aurora/codegen";

await codegen({
  schemaDir: "./schemas",
  outputDir: "./generated",
  eos: true,
});

Generated Files

Types

// generated/api/client/types.ts
export interface CreateOrderParams {
  tenantId: string;
}

export interface CreateOrderBody {
  userId: string;
  amount: number;
  currency: string;
}

export interface CreateOrderResponse {
  orderId: string;
}

export interface Order {
  orderId: string;
  tenantId: string;
  amount: number;
  status: string;
}

Client SDK

// generated/api/client/sdk.ts
import type { AuroraClient } from "@eos/types";

export interface CreateAuroraClientOptions {
  baseUrl: string;
  getAuthToken?: () => string | null;
}

export function createAuroraClient(
  options: CreateAuroraClientOptions
): AuroraClient {
  return {
    async get<TResponse>(path: string, options?: RequestInit): Promise<TResponse> {
      const url = `${options.baseUrl}${path}`;
      const headers = new Headers(options?.headers);

      if (options.getAuthToken) {
        const token = options.getAuthToken();
        if (token) {
          headers.set("Authorization", `Bearer ${token}`);
        }
      }

      const response = await fetch(url, { ...options, headers });
      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${await response.text()}`);
      }
      return response.json();
    },

    post<TInput, TResponse>(
      path: string,
      body: TInput,
      options?: RequestInit
    ): Promise<TResponse> {
    },

    put<TInput, TResponse>(
      path: string,
      body: TInput,
      options?: RequestInit
    ): Promise<TResponse> {
    },

    patch<TInput, TResponse>(
      path: string,
      body: TInput,
      options?: RequestInit
    ): Promise<TResponse> {
    },

    delete<TResponse>(path: string, options?: RequestInit): Promise<TResponse> {
    },

    subscribe<TData>(
      path: string,
      onMessage: (data: TData) => void,
      onError?: (error: unknown) => void
    ): () => void {
    },
  };
}

Query Hooks

// generated/eos/hooks/queries/index.ts
import { useQuery } from "@eos/core/hooks";
import { useEosContext } from "@eos/core/providers";
import type { GetOrderParams, Order } from "../../api/client/types";

export function useGetOrder(
  params: GetOrderParams | undefined,
  options?: {
    enabled?: boolean;
    onSuccess?(data: Order): void;
    onError?(error: unknown): void;
  }
) {
  const { auroraClient } = useEosContext();

  return useQuery<Order>(
    async () => {
      return auroraClient.get<Order>(
        `/api/v1/${params.tenantId}/orders/${params.orderId}`
      );
    },
    {
      enabled: !!params,
      ...options,
    }
  );
}

Mutation Hooks

// generated/eos/hooks/mutations/index.ts
import { useMutation } from "@eos/core/hooks";
import { useEosContext } from "@eos/core/providers";
import type { CreateOrderParams, CreateOrderBody, CreateOrderResponse } from "../../api/client/types";

export function useCreateOrder(
  options?: {
    onSuccess?(data: CreateOrderResponse): void;
    onError?(error: unknown): void;
  }
) {
  const { auroraClient } = useEosContext();

  return useMutation<CreateOrderParams & CreateOrderBody, CreateOrderResponse>(
    async (input) => {
      const { tenantId, ...body } = input;
      return auroraClient.post<CreateOrderBody, CreateOrderResponse>(
        `/api/v1/${tenantId}/orders`,
        body
      );
    },
    options
  );
}

Subscription Hooks

// generated/eos/hooks/subscriptions/index.ts
import { useSubscription } from "@eos/core/hooks";
import type { OrderUpdateEvent } from "../../api/client/types";

export function useOrderUpdates(
  tenantId: string,
  orderId: string,
  options?: {
    enabled?: boolean;
  }
) {
  return useSubscription<OrderUpdateEvent>(
    `/api/v1/${tenantId}/orders/${orderId}/updates`,
    options
  );
}

Route Metadata

// generated/eos/routes.ts
export interface RouteMetadata {
  path: string;
  method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
  params: Record<string, unknown>;
  tenantParam?: string;
}

export const ROUTES = {
  createOrder: {
    path: "/api/v1/:tenantId/orders",
    method: "POST" as const,
    params: { tenantId: "string" },
    tenantParam: "tenantId",
  },
  getOrder: {
    path: "/api/v1/:tenantId/orders/:orderId",
    method: "GET" as const,
    params: { tenantId: "string", orderId: "string" },
    tenantParam: "tenantId",
  },
} as const;

export function buildRoute<K extends keyof typeof ROUTES>(
  routeKey: K,
  values: Record<string, string>
): string {
  const route = ROUTES[routeKey];
  let path = route.path;

  for (const [key, value] of Object.entries(values)) {
    path = path.replace(`:${key}`, value);
  }

  return path;
}

License

MIT

Dependencies

Dependencies

ID Version
@aurora/codegen 0.3.3

Development Dependencies

ID Version
typescript ^5.0.0
Details
npm
2025-12-27 10:44:50 +00:00
10
latest
5.3 KiB
Assets (1)
Versions (3) View all
0.3.3 2025-12-27
0.3.2 2025-12-27
0.3.1 2025-12-27