Skip to content

Commit

Permalink
refactor: refactor utils (#68)
Browse files Browse the repository at this point in the history
* refactor: move types out of utils

* refactor: abstract saveFile and formatTypeScript utils
  • Loading branch information
toomuchdesign authored Sep 13, 2023
1 parent af2813c commit 16f7204
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 37 deletions.
6 changes: 2 additions & 4 deletions src/openapiToTsJsonSchema.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import fs from 'fs/promises';
import { existsSync } from 'fs';
import path from 'path';
import path from 'node:path';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import YAML from 'yaml';
import get from 'lodash.get';
import {
clearFolder,
makeJsonSchemaFiles,
REF_SYMBOL,
SchemaPatcher,
convertOpenApiToJsonSchema,
convertOpenApiParameters,
SchemaMetaDataMap,
JSONSchema,
addSchemaToMetaData,
pathToRef,
} from './utils';
import type { SchemaPatcher, SchemaMetaDataMap, JSONSchema } from './types';

export async function openapiToTsJsonSchema({
openApiSchema: openApiSchemaRelative,
Expand Down
File renamed without changes.
12 changes: 7 additions & 5 deletions src/utils/addSchemaToMetaData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import path from 'path';
import path from 'node:path';
// @ts-expect-error no type defs for namify
import namify from 'namify';
import filenamify from 'filenamify';
import {
SchemaMetaDataMap,
SchemaMetaData,
JSONSchema,
replaceInlinedRefsWithStringPlaceholder,
patchJsonSchema,
SchemaPatcher,
refToPath,
} from '.';
import type {
SchemaMetaDataMap,
SchemaMetaData,
JSONSchema,
SchemaPatcher,
} from '../types';

/*
* Just an utility function to add entries to SchemaMetaDataMap Map keyed by ref
Expand Down
2 changes: 1 addition & 1 deletion src/utils/convertOpenApiParameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { convertParametersToJSONSchema } from 'openapi-jsonschema-parameters';
import type { JSONSchema } from '.';
import type { JSONSchema } from '../types';

/**
* Parameters field can only be found in:
Expand Down
2 changes: 1 addition & 1 deletion src/utils/convertOpenApiToJsonSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fromSchema } from '@openapi-contrib/openapi-schema-to-json-schema';
import { OpenApiSchema } from '.';
import type { OpenApiSchema } from '../types';

export function convertOpenApiToJsonSchema(schema: OpenApiSchema) {
/**
Expand Down
File renamed without changes.
12 changes: 4 additions & 8 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { patchJsonSchema } from './patchJsonSchema';
export { clearFolder } from './clearFolder';
export { jsonSchemaToTsConst } from './jsonSchemaToTsConst';
export { convertOpenApiParameters } from './convertOpenApiParameters';
export { convertOpenApiToJsonSchema } from './convertOpenApiToJsonSchema';
Expand All @@ -13,12 +12,9 @@ export {
} from './refReplacementUtils';
export { replaceInlinedRefsWithStringPlaceholder } from './replaceInlinedRefsWithStringPlaceholder';
export { replacePlaceholdersWithImportedSchemas } from './jsonSchemaToTsConst/replacePlaceholdersWithImportedSchemas';
export type {
JSONSchema,
OpenApiSchema,
SchemaPatcher,
SchemaMetaData,
SchemaMetaDataMap,
} from './types';
export { addSchemaToMetaData } from './addSchemaToMetaData';

export { clearFolder } from './clearFolder';
export { makeRelativePath } from './makeRelativePath';
export { formatTypeScript } from './formatTypeScript';
export { saveFile } from './saveFile';
9 changes: 3 additions & 6 deletions src/utils/jsonSchemaToTsConst/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import prettier from 'prettier';
import { stringify } from 'comment-json';
import { replacePlaceholdersWithImportedSchemas } from './replacePlaceholdersWithImportedSchemas';
import type { SchemaMetaDataMap, SchemaMetaData } from '../';
import { formatTypeScript } from '../';
import type { SchemaMetaDataMap, SchemaMetaData } from '../../types';

export async function jsonSchemaToTsConst({
metaData,
Expand All @@ -25,9 +25,6 @@ export async function jsonSchemaToTsConst({

tsSchema = tsSchema + `\n\nexport const $id = "${metaData.schemaId}";`;

const formattedSchema = await prettier.format(tsSchema, {
parser: 'typescript',
});

const formattedSchema = await formatTypeScript(tsSchema);
return formattedSchema;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SchemaMetaDataMap, makeRelativePath, PLACEHOLDER_REGEX } from '..';
import { makeRelativePath, PLACEHOLDER_REGEX } from '..';
import type { SchemaMetaDataMap } from '../../types';

/**
* Replace Refs placeholders with imported schemas
Expand Down
9 changes: 4 additions & 5 deletions src/utils/makeJsonSchemaFiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs/promises';
import { jsonSchemaToTsConst, SchemaMetaDataMap } from '.';
import { jsonSchemaToTsConst, saveFile } from '.';
import type { SchemaMetaDataMap } from '../types';

/**
* Save TS JSON schema with the expected naming conventions
Expand All @@ -15,8 +15,7 @@ export async function makeJsonSchemaFiles({
schemaMetaDataMap,
});

const { schemaAbsoluteDirName, schemaAbsolutePath } = metaData;
await fs.mkdir(schemaAbsoluteDirName, { recursive: true });
await fs.writeFile(schemaAbsolutePath, tsSchema);
const { schemaAbsolutePath } = metaData;
await saveFile({ path: [schemaAbsolutePath], data: tsSchema });
}
}
2 changes: 1 addition & 1 deletion src/utils/makeRelativePath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';

/**
* Evaluate the relative path from/to the given absolute paths
Expand Down
2 changes: 1 addition & 1 deletion src/utils/patchJsonSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import traverse from 'json-schema-traverse';
import type { JSONSchema, SchemaPatcher } from './';
import type { JSONSchema, SchemaPatcher } from '../types';

export function patchJsonSchema(
schema: JSONSchema,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/replaceInlinedRefsWithStringPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mapObject from 'map-obj';
import { JSONSchema, refToPlaceholder, REF_SYMBOL } from '.';
import { refToPlaceholder, REF_SYMBOL } from '.';
import type { JSONSchema } from '../types';

function isObject(value: unknown): value is Record<string | symbol, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
Expand Down
15 changes: 15 additions & 0 deletions src/utils/saveFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fs from 'fs/promises';
import nodePath from 'node:path';

export async function saveFile({
path,
data,
}: {
path: string[];
data: string;
}): Promise<void> {
const absolutePath = nodePath.resolve(...path);
const dirname = nodePath.dirname(absolutePath);
await fs.mkdir(dirname, { recursive: true });
await fs.writeFile(absolutePath, data);
}
3 changes: 2 additions & 1 deletion test/dereferencing.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'path';
import fs from 'fs/promises';
import { describe, it, expect } from 'vitest';
import { importFresh, formatTypeScript } from './test-utils';
import { importFresh } from './test-utils';
import { openapiToTsJsonSchema } from '../src';
import { formatTypeScript } from '../src/utils';

const fixtures = path.resolve(__dirname, 'fixtures');

Expand Down
3 changes: 2 additions & 1 deletion test/experimentalImportRefs.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'path';
import fs from 'fs/promises';
import { describe, it, expect } from 'vitest';
import { importFresh, formatTypeScript } from './test-utils';
import { importFresh } from './test-utils';
import { openapiToTsJsonSchema } from '../src';
import { formatTypeScript } from '../src/utils';

const fixtures = path.resolve(__dirname, 'fixtures');

Expand Down
1 change: 0 additions & 1 deletion test/test-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { importFresh } from './importFresh';
export { formatTypeScript } from './formatTypeScript';

0 comments on commit 16f7204

Please sign in to comment.