Skip to content

migrate from openapi typescript codegen to hey-api-openapi-ts #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/update-sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
type: string
required: true
branch:
description: 'Branch of flackonInc/docupilot-appserver which openapi.yaml should be downloaded'
description: 'Branch of docupilot/docupilot-appserver which openapi.yaml should be downloaded'
type: string
default: 'main'
required: true
Expand Down Expand Up @@ -50,7 +50,7 @@ jobs:
-H "Accept: application/vnd.github.raw" \
-H "Authorization: Bearer ${{ secrets.PRIVATE_DEPENDENCIES_PAT }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/flackonInc/docupilot-appserver/contents/openapi.yaml?ref=${{ inputs.branch }} \
https://api.github.com/repos/docupilot/docupilot-appserver/contents/openapi.yaml?ref=${{ inputs.branch }} \
-o openapi.yaml

- run: git diff
Expand Down
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@flackonInc:registry=https://npm.pkg.github.com
@docupilot:registry=https://npm.pkg.github.com
79 changes: 67 additions & 12 deletions generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

'use strict';

const { program } = require('commander');
const pkg = require('./package.json');
const fs = require('fs');
const yaml = require('yaml');
const OpenAPI = require('@flackonInc/openapi-typescript-codegen');
import pkg from './package.json' assert { type: "json" };

import { program } from 'commander';

import fs from 'fs';

import yaml from 'yaml';

import * as OpenAPI from '@hey-api/openapi-ts';

import * as path from 'path';


const params = program
.name('openapi')
Expand Down Expand Up @@ -45,26 +52,74 @@ for (let pathSpec of Object.values(spec.paths)) {
}

if (OpenAPI) {
OpenAPI.generate({
OpenAPI.createClient({
input: spec,
output: params.output,
httpClient: params.client,
clientName: params.name,
client: params.client,
name: params.name,
useOptions: params.useOptions,
useUnionTypes: params.useUnionTypes,
exportCore: JSON.parse(params.exportCore) === true,
exportServices: JSON.parse(params.exportServices) === true,
exportModels: JSON.parse(params.exportModels) === true,
exportSchemas: JSON.parse(params.exportSchemas) === true,
indent: params.indent,
postfix: params.postfix,
format: params.indent,
postfixServices: params.postfix,
request: params.request,
plugins: [
{
asClass: true,
name: '@hey-api/sdk',
},
],
})
.then(() => {
.then(async() => {
// Path to the TypeScript file
const tsFilePath = path.resolve('.', 'src/api/types.gen.ts');
await updateRequestbodyWithOmitReadonly(tsFilePath);
process.exit(0);
})
.catch((error) => {
console.error(error);
process.exit(1);
});
}





async function updateRequestbodyWithOmitReadonly(filePath) {
try {
// Check if the file exists
if (!fs.existsSync(filePath)) {
throw new Error(`File not found: ${filePath}`);
}

// Read the TypeScript file
let content = await fs.promises.readFile(filePath, 'utf-8');

// Add import at the top if not already present
const importStatement = `import { OmitReadonly } from '../utils/OmitReadonly';`;
if (!content.includes(importStatement)) {
content = `${importStatement}\n${content}`;
}

// Replace all occurrences of `requestBody: abc`, `requestBody?: abc`,
// `requestBody: Array<abc>`, or `requestBody?: Array<abc>`
const updatedContent = content.replace(
/requestBody(\??):\s*([A-Za-z0-9_<>]+)/g,
(match, optional, type) => `requestBody${optional}: OmitReadonly<${type}>`
);

// Write the updated content back to the file
await fs.promises.writeFile(filePath, updatedContent, 'utf-8');
console.log('Import added (if not already present) and all occurrences replaced successfully.');
} catch (error) {
console.error('Error:', error.message);
}
}





Loading