Skip to content

Commit b3d786e

Browse files
committed
don't use the assert module
1 parent 8f7e72e commit b3d786e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/to-typescript.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import assert from 'assert';
21
import type { MongoDBJSONSchema } from './types';
32

43
function getBSONType(property: MongoDBJSONSchema): string | string[] | undefined {
@@ -17,6 +16,12 @@ function isBSONPrimitive(property: MongoDBJSONSchema): boolean {
1716
return !(isBSONArrayProperty(property) || isBSONObjectProperty(property));
1817
}
1918

19+
function assertIsDefined<T>(value: T): asserts value is NonNullable<T> {
20+
if (value === undefined || value === null) {
21+
throw new Error(`${value} is not defined`);
22+
}
23+
}
24+
2025
function toTypeName(type: string): string {
2126
// JSON Schema types
2227
if (type === 'string') {
@@ -100,7 +105,7 @@ function indentSpaces(indent: number) {
100105
}
101106

102107
function arrayType(types: string[]) {
103-
assert(types.length, 'expected types');
108+
assertIsDefined(types.length);
104109

105110
if (types.length === 1) {
106111
return `${types[0]}[]`;
@@ -115,22 +120,22 @@ function toTypescriptType(properties: Record<string, MongoDBJSONSchema>, indent:
115120
}
116121

117122
if (isBSONArrayProperty(schema)) {
118-
assert(schema.items, 'expected schema.items');
123+
assertIsDefined(schema.items);
119124
return `${indentSpaces(indent)}${propertyName}?: ${arrayType([...uniqueTypes(schema.items)])}`;
120125
}
121126

122127
if (isBSONObjectProperty(schema)) {
123-
assert(schema.properties, 'expected schema.properties');
128+
assertIsDefined(schema.properties);
124129
return `${indentSpaces(indent)}${propertyName}?: ${toTypescriptType(schema.properties, indent + 1)}`;
125130
}
126131

127-
assert(false, 'this should not be possible');
132+
throw new Error('We should never get here');
128133
});
129134

130135
return `{\n${eachFieldDefinition.join(';\n')}\n${indentSpaces(indent - 1)}}`;
131136
}
132137
export function toTypescriptTypeDefinition(databaseName: string, collectionName: string, schema: MongoDBJSONSchema): string {
133-
assert(schema.properties, 'expected schama.properties');
138+
assertIsDefined(schema.properties);
134139

135140
return `module ${databaseName} {
136141
type ${collectionName} = ${toTypescriptType(schema.properties, 2)};

0 commit comments

Comments
 (0)