Skip to content

[TS SDK] defineConfig() and PluginConfig interface #509

@widal001

Description

@widal001

Summary

Implement defineConfig() which accepts SchemaExtensions and returns a PluginConfig with .extensions and .schemas. The function composes Zod schemas from the provided extensions using withCustomFields(), so that TypeScript users get fully typed schemas at authoring time with no generation step.

Example

// plugins/combined/commongrants.config.ts
import { defineConfig, mergeExtensions } from "@commongrants/sdk/extensions";
import type { SchemaExtensions } from "@commongrants/sdk/extensions";
import hhs from "@commongrants/hhs-plugin";
import ca from "@commongrants/california-plugin";

const localExtensions = {
  Opportunity: {
    eligibilityType: {
      fieldType: "array",
      description: "Types of eligible organizations",
      valueSchema: z.array(z.string()),
    },
  },
} as const satisfies SchemaExtensions;

export default defineConfig(
  compose(
    [hhs.extensions, ca.extensions, localExtensions],
    { onConflict: "error" },
  ),
);
// Usage — schemas are typed immediately, no generation step
import combined from "../plugins/combined/commongrants.config";

const opp = combined.schemas.Opportunity.parse(apiResponse);
opp.customFields.programArea.value;          // string
opp.customFields.eligibilityType.value;      // string[]

Acceptance criteria

  • PluginConfig interface is exported with extensions: SchemaExtensions and schemas containing typed Zod schemas keyed by base model name
  • defineConfig() accepts SchemaExtensions directly (not wrapped in an options object) and returns a PluginConfig
  • as const satisfies SchemaExtensions inputs produce fully inferred literal types on .schemas output — field keys and value types are preserved through to z.infer
  • Any directory with a cg.config.ts that exports a PluginConfig can be imported as a plugin by another config, with .extensions available for further composition
  • Integration test:
    • define extensions
    • call defineConfig()
    • assert config.schemas.Opportunity.parse() validates correct payloads and rejects invalid ones

Metadata

Metadata

Assignees

Labels

sdkIssue or PR related to our SDKsts-sdkRelated to TypeScript SDKtypescriptIssue or PR related to TypeScript tooling

Type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions