Skip to content

Commit

Permalink
feat: update typescript defs
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Jan 29, 2024
1 parent d7bc9e3 commit 24b6b62
Show file tree
Hide file tree
Showing 13 changed files with 151,235 additions and 4,256 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
147,529 changes: 147,529 additions & 0 deletions .yarn/releases/yarn-classic.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-classic.cjs
2 changes: 1 addition & 1 deletion net/lib/PendingMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class PendingMessages<IPayload> {
private readonly pendingRequestsById = new Map<string, IResolvablePromise<IPayload>>();
private dontCancelIds = new Set<string>();

constructor(private marker: string = `\n${'------CONNECTION'.padEnd(50, '-')}\n`) {}
constructor(private marker = `\n${'------CONNECTION'.padEnd(50, '-')}\n`) {}

public cancel(error: CanceledPromiseError): void {
for (const id of this.pendingRequestsById.keys()) {
Expand Down
2 changes: 1 addition & 1 deletion net/lib/TransportBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class TransportBridge {
public transportToClient = new EmittingTransportToClient();
public transportToCore = new EmittingTransportToCore();

constructor(public shouldSerialize = false, private serializationMarker: string = 'DIRECT') {
constructor(public shouldSerialize = false, private serializationMarker = 'DIRECT') {
this.transportToClient.on('outbound', msg => this.sendToTransport(msg, this.transportToCore));
this.transportToCore.on('outbound', msg => this.sendToTransport(msg, this.transportToClient));
}
Expand Down
2 changes: 1 addition & 1 deletion package.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"test:build": "cross-env NODE_ENV=test jest"
},
"devDependencies": {
"jest": "^29.5.0",
"jest": "^29.7.0",
"cross-env": "^7.0.3"
}
}
30 changes: 22 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,32 @@
"version:bump": "ulx-repo-version-bump"
},
"devDependencies": {
"@commitlint/cli": "^17.6.3",
"@commitlint/config-conventional": "^17.6.3",
"@types/jest": "^29.5.1",
"@commitlint/cli": "^18.6.0",
"@commitlint/config-conventional": "^18.6.0",
"@types/jest": "^29.5.11",
"@types/node": "^16.18.31",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@ulixee/repo-tools": "^1.0.26",
"cross-env": "^7.0.3",
"husky": "^8.0.3",
"jest": "^29.5.0",
"lerna": "^4.0.0",
"lint-staged": "^13.2.2",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-monorepo-cop": "^1.0.2",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.1.1",
"husky": "^9.0.7",
"jest": "^29.7.0",
"lerna": "^8.0.2",
"lint-staged": "^15.2.0",
"shx": "^0.3.3",
"typescript": "~4.7.3"
"typescript": "~5.3.3"
},
"lint-staged": {
"*.ts": [
Expand Down
19 changes: 11 additions & 8 deletions schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BaseSchema from './lib/BaseSchema';
import BaseSchema, { IBaseConfig } from './lib/BaseSchema';
import NumberSchema, { INumberSchemaConfig } from './lib/NumberSchema';
import StringSchema, { IStringSchemaConfig } from './lib/StringSchema';
import BigintSchema, { IBigintSchemaConfig } from './lib/BigintSchema';
Expand Down Expand Up @@ -39,8 +39,8 @@ export type IRecordSchemaType<T extends Record<string, BaseSchema<any, boolean>>
? { [K in keyof P]: P[K] }
: never;

export type ExtractSchemaType<T> = T extends BaseSchema<any, boolean>
? T['$type']
export type ExtractSchemaType<T> = T extends BaseSchema<infer U, boolean>
? U
: T extends Record<string, BaseSchema<any, boolean>>
? IRecordSchemaType<T>
: unknown;
Expand Down Expand Up @@ -96,12 +96,15 @@ export function record<Values extends BaseSchema<any, boolean>, TOptional extend
}

export function object<
O extends Record<string, BaseSchema<any, boolean>>,
TOptional extends boolean = boolean,
S extends BaseSchema<any, boolean> & IBaseConfig<TOptional> = BaseSchema<any, boolean> & IBaseConfig<TOptional>,
O extends Record<string, S> = Record<string, S>,
>(fields: O): ObjectSchema<O, false, S>;
export function object<
S extends BaseSchema<any, boolean> = BaseSchema<any, boolean>,
O extends Record<string, S> = Record<string, S>,
TOptional extends boolean = false,
>(config: IObjectSchemaConfig<O, TOptional>): ObjectSchema<O, false>;
export function object<O extends Record<string, BaseSchema<any, boolean>>>(
fields: O,
): ObjectSchema<O, false>;
>(config: IObjectSchemaConfig<O, S, TOptional>): ObjectSchema<O, TOptional,S>;
export function object(fieldsOrConfig): ObjectSchema<any> {
if (
!fieldsOrConfig.fields ||
Expand Down
17 changes: 8 additions & 9 deletions schema/lib/ObjectSchema.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import assert = require('assert');
import BaseSchema, { IBaseConfig } from './BaseSchema';
import { ExtractSchemaType } from '../index';
import BaseSchema, { IBaseConfig } from './BaseSchema';

type ISchemaRecord<
O extends Record<string, TSchema>,
TSchema extends BaseSchema<any, boolean> = BaseSchema<any, boolean>,
> = {
type ISchemaRecord<O extends Record<string, BaseSchema<any, boolean>>> = {
[T in keyof O]: O[T];
};

export interface IObjectSchemaConfig<
O extends Record<string, BaseSchema<any, boolean>>,
O extends Record<string, S>,
S extends BaseSchema<any, boolean> = BaseSchema<any, boolean>,
TOptional extends boolean = boolean,
> extends IBaseConfig<TOptional> {
fields: ISchemaRecord<O>;
}

export default class ObjectSchema<
O extends Record<string, BaseSchema<any, boolean>>,
O extends Record<string, S>,
TOptional extends boolean = boolean,
> extends BaseSchema<ExtractSchemaType<O>, TOptional, IObjectSchemaConfig<O, TOptional>> {
S extends BaseSchema<any, boolean> = BaseSchema<any, boolean>,
> extends BaseSchema<ExtractSchemaType<O>, TOptional, IObjectSchemaConfig<O, S, TOptional>> {
readonly typeName = 'object';
fields: O;

constructor(config: IObjectSchemaConfig<O, TOptional>) {
constructor(config: IObjectSchemaConfig<O, S, TOptional>) {
super(config);
assert(config.fields, 'You must configure the fields for this object');
assert(
Expand Down
2 changes: 1 addition & 1 deletion schema/package.dist.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"devDependencies": {
"typescript": "^5.0.4"
"typescript": "^5.3.3"
}
}
2 changes: 1 addition & 1 deletion schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"moment": "^2.29.4"
},
"devDependencies": {
"typescript": "^5.0.4"
"typescript": "^5.3.3"
}
}
35 changes: 27 additions & 8 deletions schema/test/Object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
buffer,
date,
ExtractSchemaType,
FilterOptionalKeys,
FilterRequiredKeys,
number,
object,
string,
Expand All @@ -15,12 +13,15 @@ import ObjectSchema from '../lib/ObjectSchema';

test('should be able to create an object schema', () => {
const schema = object({
fields: {
one: boolean(),
two: string(),
three: number({ optional: true }),
},
one: boolean(),
two: string(),
three: number({ optional: true }),
});
const testOptional: ExtractSchemaType<typeof schema> = {
one: true,
two: 'two',
};
expect(testOptional).toBeTruthy();
expect(schema.validate({ one: true, two: '' }).success).toBe(true);
expect(schema.validate({ one: true, two: '', three: 1 }).success).toBe(true);
expect(schema.validate({ one: true, two: '', three: '' }).success).toBe(false);
Expand Down Expand Up @@ -62,10 +63,28 @@ test('should be able to create an object schema with nested objects', () => {
nested: {
four: 1,
},
nestedWithFields: { seven: 2n, six: new Date() },
};
expect(nestedOptionalSchema).toBeTruthy();

const nestedBadTypeField: ExtractSchemaType<typeof record> = {
two: 'two',
nested: {
four: 1,
},
// @ts-expect-error
nestedWithFields: { bad: true, seven: 2n, six: new Date() },
};
expect(nestedBadTypeField).toBeTruthy();

const nestedOptionalField: ExtractSchemaType<typeof record> = {
two: 'two',
nested: {
four: 1,
},
nestedWithFields: { seven: 2n, six: new Date() },
};
expect(nestedOptionalField).toBeTruthy();

const schema = object({
fields: {
one: boolean(),
Expand Down
Loading

0 comments on commit 24b6b62

Please sign in to comment.