Skip to content

Injecting imports for transformed types  #1997

@gresnick

Description

@gresnick

Description

This is more a basic question than a bug.

I am following the Node.js API docs with the example of replacing Date and DateTime. I would like to use a custom library (e.g. luxon) to provide these types.

My script

import ts from "typescript";
import openapiTS, { astToString } from "openapi-typescript";
import fs from "node:fs";

const DATE_TIME = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("DateTime"));
const DATE = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("Date")); 
const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull());

const localPath = new URL("../../api/openapi.yaml", import.meta.url); 
const output = await openapiTS(localPath,{
    transform(schemaObject, metadata) {
     if (["date-time", "date"].includes(schemaObject.format)) {
        return schemaObject.nullable
          ? ts.factory.createUnionTypeNode([DATE_TIME, NULL])
          : DATE_TIME;
      }
    },
  });
  fs.writeFileSync("interfaces.d.ts", astToString(output))

The resulting interfaces.d.ts file has the types mapped correctly

    schemas: {
        Id: string;
        String: string;
        /** Format: date-time */
        DateTime: DateTime;
      Date: DateTime;
      ...
   }

however DateTime is unresolved.

How do I inject import { DateTime } from 'luxon'; into interfaces.d.ts ?

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions