Skip to content

Commit 4aec16b

Browse files
committed
refactor: make interface fields readonly
BREAKING CHANGE: interface fields were made readonly
1 parent 090b725 commit 4aec16b

File tree

5 files changed

+182
-181
lines changed

5 files changed

+182
-181
lines changed

src/fs.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as fs from 'fs-extra';
22
import * as path from 'path';
33

44
export interface File {
5-
type: 'FILE';
6-
name: string;
7-
content: string;
5+
readonly type: 'FILE';
6+
readonly name: string;
7+
readonly content: string;
88
}
99

1010
export const file = (name: string, content: string): File => ({
@@ -14,9 +14,9 @@ export const file = (name: string, content: string): File => ({
1414
});
1515

1616
export interface Directory {
17-
type: 'DIRECTORY';
18-
name: string;
19-
content: FSEntity[];
17+
readonly type: 'DIRECTORY';
18+
readonly name: string;
19+
readonly content: FSEntity[];
2020
}
2121

2222
export const directory = (name: string, content: FSEntity[]): Directory => ({
@@ -28,8 +28,8 @@ export const directory = (name: string, content: FSEntity[]): Directory => ({
2828
export type FSEntity = File | Directory;
2929

3030
export interface BufferWithName {
31-
buffer: Buffer;
32-
fileName: string;
31+
readonly buffer: Buffer;
32+
readonly fileName: string;
3333
}
3434

3535
export const write = async (destination: string, entity: FSEntity): Promise<void> => {

src/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ export interface GenerateOptions {
1919
/**
2020
* Paths to spec files
2121
*/
22-
pathsToSpec: string[];
22+
readonly pathsToSpec: string[];
2323
/**
2424
* Path to output directory (should be empty)
2525
*/
26-
out: string;
26+
readonly out: string;
2727
/**
2828
* Spec serializer
2929
*/
30-
serialize: Serializer;
30+
readonly serialize: Serializer;
3131
/**
3232
* Path to prettier config
3333
*/
34-
pathToPrettierConfig?: string;
34+
readonly pathToPrettierConfig?: string;
3535
/**
3636
* Buffer to JSON converter
3737
* @param buffer - File Buffer
3838
*/
39-
fileReader: FileReader;
39+
readonly fileReader: FileReader;
4040
}
4141

4242
const cwd = process.cwd();

src/language/typescript.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AllOfSchemaObject,
33
BodyParameterObject,
44
DefinitionsObject,
5+
Dictionary,
56
NonArrayItemsObject,
67
OperationObject,
78
ParametersDefinitionsObject,
@@ -59,15 +60,15 @@ const unless = (condition: boolean, a: string): string => (condition ? '' : a);
5960
const when = (condition: boolean, a: string): string => (condition ? a : '');
6061

6162
interface Dependency {
62-
name: string;
63-
path: string;
63+
readonly name: string;
64+
readonly path: string;
6465
}
6566

6667
interface SerializedType {
67-
type: string;
68-
io: string;
69-
dependencies: Dependency[];
70-
refs: string[];
68+
readonly type: string;
69+
readonly io: string;
70+
readonly dependencies: Dependency[];
71+
readonly refs: string[];
7172
}
7273

7374
const serializedType = (type: string, io: string, dependencies: Dependency[], refs: string[]): SerializedType => ({
@@ -78,7 +79,7 @@ const serializedType = (type: string, io: string, dependencies: Dependency[], re
7879
});
7980

8081
interface SerializedParameter extends SerializedType {
81-
isRequired: boolean;
82+
readonly isRequired: boolean;
8283
}
8384
const serializedParameter = (
8485
type: string,
@@ -94,7 +95,7 @@ const serializedParameter = (
9495
refs,
9596
});
9697
interface SerializedPathParameter extends SerializedParameter {
97-
name: string;
98+
readonly name: string;
9899
}
99100
const serializedPathParameter = (
100101
name: string,
@@ -204,7 +205,7 @@ const serializeDefinition = (name: string, definition: SchemaObject, cwd: string
204205
);
205206
};
206207

207-
const serializePathGroup = (name: string, group: Record<string, PathItemObject>, cwd: string): File => {
208+
const serializePathGroup = (name: string, group: Dictionary<PathItemObject>, cwd: string): File => {
208209
const groupName = `${name}Controller`;
209210
const serialized = foldSerialized(
210211
serializeDictionary(group, (url, item) => serializePath(url, item, groupName, cwd)),
@@ -745,7 +746,7 @@ const serializeNonArrayItemsObject = (items: NonArrayItemsObject): SerializedTyp
745746
}
746747
};
747748

748-
const serializeDictionary = <A, B>(dictionary: Record<string, A>, serializeValue: (name: string, value: A) => B): B[] =>
749+
const serializeDictionary = <A, B>(dictionary: Dictionary<A>, serializeValue: (name: string, value: A) => B): B[] =>
749750
Object.keys(dictionary).map(name => serializeValue(name, dictionary[name]));
750751

751752
const getIOName = (name: string): string => `${name}IO`;
@@ -774,13 +775,13 @@ const client = `
774775
import { left } from 'fp-ts/lib/Either';
775776
776777
export interface APIRequest {
777-
url: string;
778-
query?: object;
779-
body?: unknown;
778+
readonly url: string;
779+
readonly query?: object;
780+
readonly body?: unknown;
780781
};
781782
782783
export interface FullAPIRequest extends APIRequest {
783-
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
784+
readonly method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
784785
};
785786
786787
export interface APIClient {

0 commit comments

Comments
 (0)