Skip to content

Commit

Permalink
Merge pull request #1172 from hey-api/feat/services-parser
Browse files Browse the repository at this point in the history
refactor: move out legacy parser functions
  • Loading branch information
mrlubos authored Oct 20, 2024
2 parents 4ff2429 + ea51130 commit 25e0331
Show file tree
Hide file tree
Showing 42 changed files with 354 additions and 306 deletions.
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/generate/__tests__/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { writeFileSync } from 'node:fs';
import { describe, expect, it, vi } from 'vitest';

import { setConfig } from '../../utils/config';
import { generateClientClass } from '../class';
import { generateLegacyClientClass } from '../class';
import { mockTemplates, openApi } from './mocks';

vi.mock('node:fs');

describe('generateClientClass', () => {
describe('generateLegacyClientClass', () => {
it('writes to filesystem', async () => {
setConfig({
client: {
Expand All @@ -33,15 +33,15 @@ describe('generateClientClass', () => {
useOptions: true,
});

const client: Parameters<typeof generateClientClass>[2] = {
const client: Parameters<typeof generateLegacyClientClass>[2] = {
models: [],
server: 'http://localhost:8080',
services: [],
types: {},
version: 'v1',
};

await generateClientClass(openApi, './dist', client, mockTemplates);
await generateLegacyClientClass(openApi, './dist', client, mockTemplates);

expect(writeFileSync).toHaveBeenCalled();
});
Expand Down
18 changes: 9 additions & 9 deletions packages/openapi-ts/src/generate/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import path from 'node:path';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { setConfig } from '../../utils/config';
import { generateCore } from '../core';
import { generateLegacyCore } from '../core';
import { mockTemplates } from './mocks';

vi.mock('node:fs');

describe('generateCore', () => {
let templates: Parameters<typeof generateCore>[2];
describe('generateLegacyCore', () => {
let templates: Parameters<typeof generateLegacyCore>[2];
beforeEach(() => {
templates = mockTemplates;
});

it('writes to filesystem', async () => {
const client: Parameters<typeof generateCore>[1] = {
const client: Parameters<typeof generateLegacyCore>[1] = {
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('generateCore', () => {
useOptions: true,
});

await generateCore('/', client, templates);
await generateLegacyCore('/', client, templates);

expect(writeFileSync).toHaveBeenCalledWith(
path.resolve('/', '/OpenAPI.ts'),
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('generateCore', () => {
});

it('uses client server value for base', async () => {
const client: Parameters<typeof generateCore>[1] = {
const client: Parameters<typeof generateLegacyCore>[1] = {
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('generateCore', () => {
useOptions: true,
});

await generateCore('/', client, templates);
await generateLegacyCore('/', client, templates);

expect(templates.core.settings).toHaveBeenCalledWith({
$config: config,
Expand All @@ -118,7 +118,7 @@ describe('generateCore', () => {
});

it('uses custom value for base', async () => {
const client: Parameters<typeof generateCore>[1] = {
const client: Parameters<typeof generateLegacyCore>[1] = {
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('generateCore', () => {
useOptions: true,
});

await generateCore('/', client, templates);
await generateLegacyCore('/', client, templates);

expect(templates.core.settings).toHaveBeenCalledWith({
$config: config,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/generate/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('generateIndexFile', () => {
}),
};

await generateIndexFile({ files });
generateIndexFile({ files });

files.index.write();

Expand Down
7 changes: 3 additions & 4 deletions packages/openapi-ts/src/generate/__tests__/output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { describe, expect, it, vi } from 'vitest';

import type { Client } from '../../types/client';
import { setConfig } from '../../utils/config';
import { generateOutput } from '../output';
import { generateLegacyOutput } from '../output';
import { mockTemplates, openApi } from './mocks';

vi.mock('node:fs');

describe('generateOutput', () => {
describe('generateLegacyOutput', () => {
it('writes to filesystem', async () => {
setConfig({
client: {
Expand Down Expand Up @@ -42,9 +42,8 @@ describe('generateOutput', () => {
version: 'v1',
};

await generateOutput({
await generateLegacyOutput({
client,
context: undefined,
openApi,
templates: mockTemplates,
});
Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/generate/__tests__/schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { describe, expect, it, vi } from 'vitest';
import type { OpenApiV3Schema } from '../../openApi';
import type { Files } from '../../types/utils';
import { setConfig } from '../../utils/config';
import { generateSchemas } from '../schemas';
import { generateLegacySchemas } from '../schemas';
import { openApi } from './mocks';

vi.mock('node:fs');

describe('generateSchemas', () => {
describe('generateLegacySchemas', () => {
it('writes to filesystem', async () => {
setConfig({
client: {
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('generateSchemas', () => {

const files: Files = {};

await generateSchemas({ files, openApi });
await generateLegacySchemas({ files, openApi });

files.schemas.write();

Expand Down Expand Up @@ -103,7 +103,7 @@ describe('generateSchemas', () => {

const files: Files = {};

await generateSchemas({ files, openApi });
await generateLegacySchemas({ files, openApi });

files.schemas.write();

Expand Down
20 changes: 8 additions & 12 deletions packages/openapi-ts/src/generate/__tests__/services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import type { Operation } from '../../types/client';
import type { Files } from '../../types/utils';
import { setConfig } from '../../utils/config';
import { TypeScriptFile } from '../files';
import { generateServices } from '../services';
import { generateLegacyServices } from '../services';

vi.mock('node:fs');

describe('generateServices', () => {
describe('generateLegacyServices', () => {
it('writes to filesystem', async () => {
setConfig({
client: {
Expand All @@ -36,7 +36,7 @@ describe('generateServices', () => {
useOptions: false,
});

const client: Parameters<typeof generateServices>[0]['client'] = {
const client: Parameters<typeof generateLegacyServices>[0]['client'] = {
models: [],
server: 'http://localhost:8080',
services: [
Expand Down Expand Up @@ -80,9 +80,8 @@ describe('generateServices', () => {
name: 'types.ts',
});

await generateServices({
await generateLegacyServices({
client,
context: undefined,
files,
});

Expand Down Expand Up @@ -120,7 +119,7 @@ describe('methodNameBuilder', () => {
summary: null,
};

const client: Parameters<typeof generateServices>[0]['client'] = {
const client: Parameters<typeof generateLegacyServices>[0]['client'] = {
models: [],
server: 'http://localhost:8080',
services: [
Expand Down Expand Up @@ -166,9 +165,8 @@ describe('methodNameBuilder', () => {
name: 'types.ts',
});

await generateServices({
await generateLegacyServices({
client,
context: undefined,
files,
});

Expand Down Expand Up @@ -214,9 +212,8 @@ describe('methodNameBuilder', () => {
name: 'types.ts',
});

await generateServices({
await generateLegacyServices({
client,
context: undefined,
files,
});

Expand Down Expand Up @@ -264,9 +261,8 @@ describe('methodNameBuilder', () => {
name: 'types.ts',
});

await generateServices({
await generateLegacyServices({
client,
context: undefined,
files,
});

Expand Down
9 changes: 4 additions & 5 deletions packages/openapi-ts/src/generate/__tests__/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { describe, expect, it, vi } from 'vitest';

import { setConfig } from '../../utils/config';
import { TypeScriptFile } from '../files';
import { generateTypes } from '../types';
import { generateLegacyTypes } from '../types';

vi.mock('node:fs');

describe('generateTypes', () => {
describe('generateLegacyTypes', () => {
it('writes to filesystem', async () => {
setConfig({
client: {
Expand All @@ -34,7 +34,7 @@ describe('generateTypes', () => {
useOptions: true,
});

const client: Parameters<typeof generateTypes>[0]['client'] = {
const client: Parameters<typeof generateLegacyTypes>[0]['client'] = {
models: [
{
$refs: [],
Expand Down Expand Up @@ -73,9 +73,8 @@ describe('generateTypes', () => {
}),
};

await generateTypes({
await generateLegacyTypes({
client,
context: undefined,
files,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/generate/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ensureDirSync } from './utils';
* @param client Client containing models, schemas, and services
* @param templates The loaded handlebar templates
*/
export const generateClientClass = async (
export const generateLegacyClientClass = async (
openApi: OpenApi,
outputPath: string,
client: Client,
Expand Down
37 changes: 18 additions & 19 deletions packages/openapi-ts/src/generate/client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { copyFileSync } from 'node:fs';
import path from 'node:path';

import { getConfig, isLegacyClient } from '../utils/config';
import type { Config } from '../types/config';
import { ensureDirSync, relativeModulePath } from './utils';

/**
* Returns path to the client module. When using client packages, this will be
* simply the name of the package. When bundling a client, this will be a
* relative path to the bundled client folder.
*/
export const clientModulePath = ({
config,
sourceOutput,
}: {
config: Config;
sourceOutput: string;
}) => {
const config = getConfig();

}): string => {
if (config.client.bundle) {
return relativeModulePath({
moduleOutput: 'client',
Expand All @@ -24,26 +29,20 @@ export const clientModulePath = ({
export const clientOptionsTypeName = () => 'Options';

/**
* (optional) Creates a `client.ts` file containing the same exports as the
* client package. Creates a `client` directory containing the modules from
* the client package. These files are generated only when `client.bundle` is
* set to true.
* Creates a `client` directory containing the same modules as the client package.
*/
export const generateClient = async (
outputPath: string,
moduleName: string,
) => {
const config = getConfig();

if (isLegacyClient(config) || !config.client.bundle) {
return;
}

export const generateClientBundle = ({
name,
outputPath,
}: {
name: string;
outputPath: string;
}): void => {
// create directory for client modules
const dirPath = path.resolve(outputPath, 'client');
ensureDirSync(dirPath);

const clientModulePath = path.normalize(require.resolve(moduleName));
const clientModulePath = path.normalize(require.resolve(name));
const clientModulePathComponents = clientModulePath.split(path.sep);
const clientSrcPath = [
...clientModulePathComponents.slice(
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/generate/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { Templates } from '../utils/handlebars';
* @param client Client containing models, schemas, and services
* @param templates The loaded handlebar templates
*/
export const generateCore = async (
export const generateLegacyCore = async (
outputPath: string,
client: Client,
templates: Templates,
Expand Down
6 changes: 1 addition & 5 deletions packages/openapi-ts/src/generate/indexFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import type { Files } from '../types/utils';
import { getConfig } from '../utils/config';
import { TypeScriptFile } from './files';

export const generateIndexFile = async ({
files,
}: {
files: Files;
}): Promise<void> => {
export const generateIndexFile = ({ files }: { files: Files }): void => {
const config = getConfig();

files.index = new TypeScriptFile({
Expand Down
Loading

0 comments on commit 25e0331

Please sign in to comment.