Skip to content

Commit

Permalink
Remove All Dependencies (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Apr 30, 2023
1 parent 1c2cc3e commit 211141f
Show file tree
Hide file tree
Showing 89 changed files with 1,273 additions and 628 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ node_modules*/
/lib*/
/tmp*/
# end managed by skuba

src/openapi3-ts/*
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ jobs:

- name: Lint
run: yarn lint

- name: Build
run: yarn build
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ gantry*.yml

examples/**/*.yml
examples/**/*.html
src/openapi3-ts/*
34 changes: 0 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,40 +150,6 @@ Generates the following object:
}
```

### `createDocumentJson`

In the background it calls `createDocument` but instead outputs it as a JSON string using `JSON.stringify`. It takes an optional options object as the second parameter which can customize how the JSON string is outputted.

```typescript
const document = createDocumentJson(
{
openapi: '3.1.0',
info: {
title: 'My API',
version: '1.0.0',
},
},
{ options: 2 },
);
```

### `createDocumentYaml`

In the background it calls `createDocument` but instead outputs it as a YAML string using the [yaml](https://github.com/eemeli/yaml) library. It takes an optional options object as the second parameter which can customize how the YAML string is outputted.

```typescript
const document = createDocumentYaml(
{
openapi: '3.1.0',
info: {
title: 'My API',
version: '1.0.0',
},
},
{ options: { aliasDuplicateObjects: false } },
);
```

## Usage

### Request Parameters
Expand Down
8 changes: 6 additions & 2 deletions examples/simple/createSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import fs from 'fs';
import path from 'path';

import { ZodOpenApiOperationObject, createDocumentYaml } from '../../src';
import { stringify } from 'yaml';

import { ZodOpenApiOperationObject, createDocument } from '../../src';

import {
CreateJobRequestSchema,
Expand Down Expand Up @@ -52,7 +54,7 @@ const createJobOperation: ZodOpenApiOperationObject = {
},
};

const yaml = createDocumentYaml({
const document = createDocument({
openapi: '3.1.0',
info: {
title: 'Simple API',
Expand Down Expand Up @@ -99,5 +101,7 @@ const yaml = createDocumentYaml({
},
});

const yaml = stringify(document, { aliasDuplicateObjects: false });

// eslint-disable-next-line no-sync
fs.writeFileSync(path.join(__dirname, 'openapi.yml'), yaml);
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"lib*/**/*.json"
],
"scripts": {
"build": "skuba build-package",
"build": "yarn copy:types && skuba build-package",
"copy:types": "skuba node scripts/copyTypes.ts",
"create:docs": " skuba node examples/simple/createSchema.ts && redocly build-docs examples/simple/openapi.yml --output=examples/simple/redoc-static.html",
"format": "skuba format",
"lint": "skuba lint",
Expand All @@ -31,15 +32,14 @@
"test:ci": "skuba test --coverage",
"test:watch": "skuba test --watch"
},
"dependencies": {
"openapi3-ts": "^4.1.2",
"yaml": "^2.2.2"
},
"dependencies": {},
"devDependencies": {
"@redocly/cli": "1.0.0-beta.125",
"@types/node": "^18.15.13",
"eslint-plugin-zod-openapi": "^0.0.3",
"openapi3-ts": "^4.1.2",
"skuba": "6.0.2",
"yaml": "2.2.2",
"zod": "3.21.4"
},
"peerDependencies": {
Expand Down
65 changes: 65 additions & 0 deletions scripts/copyTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { promises as fs } from 'fs';
import { join, relative } from 'path';

import { Git } from 'skuba';

async function copyDTs(src: string, dest: string): Promise<void> {
const files = await fs.readdir(src);
for (const file of files) {
const filePath = join(src, file);
const destPath = join(dest, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
await copyDTs(filePath, destPath);
} else if (filePath.endsWith('.d.ts')) {
const dirPath = join(
dest,
relative(src, filePath).split('/').slice(0, -1).join('/'),
);
await fs.mkdir(dirPath, { recursive: true });
await fs.copyFile(
filePath,
`${destPath.slice(0, destPath.length - 5)}.ts`,
);
}
}
}

async function deleteFolderRecursive(folderPath: string) {
const files = await fs.readdir(folderPath);

for (const file of files) {
const filePath = join(folderPath, file);
const stats = await fs.stat(filePath);

if (stats.isDirectory()) {
await deleteFolderRecursive(filePath);
} else {
await fs.unlink(filePath);
}
}

await fs.rmdir(folderPath);
}

async function main() {
const dir = process.cwd();

const src = join(dir, './node_modules/openapi3-ts');
const dest = join(dir, 'src/openapi3-ts');
await deleteFolderRecursive(dest);
await copyDTs(src, dest);

if (process.env.GITHUB_ACTIONS) {
const files = await Git.getChangedFiles({ dir });
if (files.length) {
throw new Error('openapi3-ts types need updating');
}
}
}

main().catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
throw error;
});
2 changes: 1 addition & 1 deletion src/create/components.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { oas31 } from 'openapi3-ts';
import { z } from 'zod';

import { extendZodWithOpenApi } from '../extendZod';
import { oas31 } from '../openapi3-ts/dist';

import {
CompleteSchemaComponent,
Expand Down
3 changes: 2 additions & 1 deletion src/create/components.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { oas30, oas31 } from 'openapi3-ts';
import { ZodRawShape, ZodType } from 'zod';

import { oas30, oas31 } from '../openapi3-ts/dist';

import {
ZodOpenApiComponentsObject,
ZodOpenApiRequestBodyObject,
Expand Down
2 changes: 1 addition & 1 deletion src/create/content.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { oas31 } from 'openapi3-ts';
import { z } from 'zod';

import { extendZodWithOpenApi } from '../extendZod';
import { oas31 } from '../openapi3-ts/dist';

import { getDefaultComponents } from './components';
import { createContent } from './content';
Expand Down
3 changes: 2 additions & 1 deletion src/create/content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { oas31 } from 'openapi3-ts';
import { ZodType } from 'zod';

import { oas31 } from '../openapi3-ts/dist';

import { ComponentsObject, CreationType } from './components';
import { ZodOpenApiContentObject, ZodOpenApiMediaTypeObject } from './document';
import { createSchemaOrRef } from './schema';
Expand Down
Loading

0 comments on commit 211141f

Please sign in to comment.