openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

Support Deno

Open AlaaZorkane opened this issue 1 year ago • 8 comments

Description

Hey everyone, thank you for the wonderful package!

Just wondering if there are any plans to support other runtimes than nodejs, more specifically Deno.

Thanks!

AlaaZorkane avatar May 09 '24 15:05 AlaaZorkane

Hey @AlaaZorkane, thanks for the message! I haven't looked into it yet and not opposed at all, but it depends on demand. The current focus are upcoming clients. Are you able to outline what changes would roughly need to happen in order to support Deno?

mrlubos avatar May 09 '24 16:05 mrlubos

ping @AlaaZorkane

mrlubos avatar Aug 11 '24 11:08 mrlubos

Hey, I just started to try this lib out using Deno, and its awesome! One thing that Deno requires is the file extension for imports, so teh current generated files will error out simply because they are missing the .ts extension in the import from statements.

Is there any current options with the configuration API to add the extension when generating files?

bombillazo avatar Aug 14 '24 03:08 bombillazo

@bombillazo That's nice to hear! There were no direct attempts at making this compatible with Deno yet. Might prioritise soon... you can use my sponsors page for list of current priorities before there's a proper roadmap https://github.com/sponsors/mrlubos

mrlubos avatar Aug 14 '24 04:08 mrlubos

On the way to support Deno, I opened this PR to help #936

bombillazo avatar Aug 14 '24 19:08 bombillazo

@bombillazo @mrlubos @AlaaZorkane Maybe what's needed is to create a Deno client, if that is a thing?

That would resolve the issue to determine how to change the generated code (especially on the import), and maybe allow to generate a Deno workspace where the current index.ts file would be renamed mod.ts and a deno.json file would be generated alongside the code with a simple:

{
  "name": "@scope/openapi",
  "version": "0.0.1",
  "exports": "./mod.ts"
}

thoroc avatar Nov 06 '24 20:11 thoroc

Also, while at it, it would be awesome if site documentation would mention deno as well. It already does this for bun.

luolong avatar Apr 29 '25 12:04 luolong

I needed this now, so here's my workaround in case anyone has a need for it: I created build.ts:

#!/usr/bin/env -S deno run --allow-read --allow-write --allow-net --allow-run --allow-env --allow-sys
// vi: ft=typescript
import { createClient, defaultPlugins } from "npm:@hey-api/openapi-ts";
import { walkSync } from "jsr:@std/fs/walk";

const output = "client";

await createClient({
  input: "some-open-api-spec",
  output,
  plugins: [
    ...defaultPlugins,
    "@hey-api/client-fetch",
    "zod",
    {
      name: "@hey-api/sdk",
      validator: true,
    },
  ],
});

for (const { path } of walkSync(output, { exts: ["ts"] })) {
  const contents = Deno.readTextFileSync(path)
    .replaceAll(/(import|export) ([\s\S]+?)from '.\/(.*?)';/gs, "$1 $2from './$3.ts';")
    .replaceAll(/\n(.+?) \| \(string & {}\)/g, "// deno-lint-ignore ban-types\n$1 | (string & {})")
    .replaceAll("from 'zod'", "from 'npm:zod'")
    .replaceAll(/\.regex\(\/(.*?)\/\)/g, ".regex($1)") // fix for https://github.com/hey-api/openapi-ts/issues/1977
    .replaceAll("from '@", "from 'npm:@");
  Deno.writeTextFileSync(path, contents);
}

Then chmod +x build.ts and done. Works well enough for my purposes, you may have to replace more strings depending on the plugins you use.

Xananax avatar May 19 '25 10:05 Xananax