Skip to content

Commit

Permalink
Add type to schemas
Browse files Browse the repository at this point in the history
Signed-off-by: Quoc-Hao Tran <[email protected]>
  • Loading branch information
haoadoreorange authored and haoadoresorange committed Dec 16, 2021
1 parent d8e23e4 commit 9adc68e
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion tsp-typescript-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"node-fetch": "^2.5.0",
"rimraf": "latest",
"when-json-met-bigint": "^0.21.0"
"when-json-met-bigint": "^0.27.0"
},
"scripts": {
"prepare": "yarn run clean && yarn run build",
Expand Down
2 changes: 1 addition & 1 deletion tsp-typescript-client/src/models/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface AnnotationCategoriesModel {
annotationCategories: string[];
}

export const AnnotationSchema: Schema = {
export const AnnotationSchema: Schema<AnnotationModel> = {
annotations: {
[Symbol.for(`any`)]: [{
duration: bigint,
Expand Down
2 changes: 1 addition & 1 deletion tsp-typescript-client/src/models/bookmark.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema } from 'when-json-met-bigint';
import { bigint } from '../protocol/serialization';

export const BookmarkSchema: Schema = {
export const BookmarkSchema: Schema<Bookmark> = {
endTime: bigint,
startTime: bigint,
};
Expand Down
4 changes: 2 additions & 2 deletions tsp-typescript-client/src/models/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Schema } from 'when-json-met-bigint';
import { assertNumber } from '../protocol/serialization';
import { OutputElementStyle } from './styles';

export const EntrySchema: Schema = {
export const EntrySchema: Schema<Entry> = {
id: assertNumber,
parentId: assertNumber,
};
Expand Down Expand Up @@ -52,7 +52,7 @@ export interface EntryHeader {
tooltip: string
}

export const EntryModelSchema = (schema: Schema): Schema => ({ entries: [schema] });
export const EntryModelSchema = <T extends Entry>(schema: Schema<T>): Schema<EntryModel<T>> => ({ entries: [schema] });

/**
* Entry model that will be returned by the server
Expand Down
2 changes: 1 addition & 1 deletion tsp-typescript-client/src/models/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Schema } from 'when-json-met-bigint';
import { assertNumber, bigint } from '../protocol/serialization';
import { TraceSchema, Trace } from './trace';

export const ExperimentSchema: Schema = {
export const ExperimentSchema: Schema<Experiment> = {
end: bigint,
nbEvents: assertNumber,
start: bigint,
Expand Down
2 changes: 1 addition & 1 deletion tsp-typescript-client/src/models/output-descriptor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema } from 'when-json-met-bigint';
import { bigint } from '../protocol/serialization';

export const OutputDescriptorSchema: Schema = {
export const OutputDescriptorSchema: Schema<OutputDescriptor> = {
end: bigint,
start: bigint,
};
Expand Down
2 changes: 1 addition & 1 deletion tsp-typescript-client/src/models/response/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export enum ResponseStatus {
CANCELLED = 'CANCELLED'
}

export const GenericResponseSchema = (schema: Schema): Schema => ({ model: schema });
export const GenericResponseSchema = <T>(schema: Schema<T>): Schema<GenericResponse<T>> => ({ model: schema });

/**
* Generic response that contains a model
Expand Down
8 changes: 4 additions & 4 deletions tsp-typescript-client/src/models/table.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema } from 'when-json-met-bigint';
import { assertNumber } from '../protocol/serialization';

export const ColumnHeaderEntrySchema: Schema = {
export const ColumnHeaderEntrySchema: Schema<ColumnHeaderEntry> = {
id: assertNumber,
};

Expand Down Expand Up @@ -30,7 +30,7 @@ export interface ColumnHeaderEntry {
type: string;
}

export const CellSchema: Schema = {
export const CellSchema: Schema<Cell> = {
tags: assertNumber,
};

Expand All @@ -49,7 +49,7 @@ export interface Cell {
tags?: number;
}

export const LineSchema = {
export const LineSchema: Schema<Line> = {
cells: [CellSchema],
index: assertNumber,
tags: assertNumber,
Expand All @@ -75,7 +75,7 @@ export interface Line {
tags?: number;
}

export const TableModelSchema = {
export const TableModelSchema: Schema<TableModel> = {
columnIds: [assertNumber],
lines: [LineSchema],
lowIndex: assertNumber,
Expand Down
10 changes: 5 additions & 5 deletions tsp-typescript-client/src/models/timegraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assertNumber, bigint } from '../protocol/serialization';
import { Entry } from './entry';
import { OutputElementStyle } from './styles';

export const TimeGraphEntrySchema: Schema = {
export const TimeGraphEntrySchema: Schema<TimeGraphEntry> = {
end: bigint,
id: assertNumber,
parentId: assertNumber,
Expand All @@ -25,7 +25,7 @@ export interface TimeGraphEntry extends Entry {
end: bigint;
}

const TimeGraphStateSchema: Schema = {
const TimeGraphStateSchema: Schema<TimeGraphState> = {
end: bigint,
start: bigint,
tags: assertNumber,
Expand Down Expand Up @@ -61,7 +61,7 @@ export interface TimeGraphState {
style?: OutputElementStyle;
}

export const TimeGraphRowSchema: Schema = {
export const TimeGraphRowSchema: Schema<TimeGraphRow> = {
entryId: assertNumber,
states: [TimeGraphStateSchema],
};
Expand All @@ -81,7 +81,7 @@ export interface TimeGraphRow {
states: TimeGraphState[];
}

export const TimeGraphModelSchema: Schema = {
export const TimeGraphModelSchema: Schema<TimeGraphModel> = {
rows: [TimeGraphRowSchema],
};

Expand All @@ -92,7 +92,7 @@ export interface TimeGraphModel {
rows: TimeGraphRow[];
}

export const TimeGraphArrowSchema: Schema = {
export const TimeGraphArrowSchema: Schema<TimeGraphArrow> = {
end: bigint,
sourceId: assertNumber,
start: bigint,
Expand Down
2 changes: 1 addition & 1 deletion tsp-typescript-client/src/models/trace.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema } from 'when-json-met-bigint';
import { assertNumber, bigint } from '../protocol/serialization';

export const TraceSchema: Schema = {
export const TraceSchema: Schema<Trace> = {
end: bigint,
nbEvents: assertNumber,
start: bigint,
Expand Down
4 changes: 2 additions & 2 deletions tsp-typescript-client/src/models/xy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema } from 'when-json-met-bigint';
import { assertNumber, number } from '../protocol/serialization';

export const XYSeriesSchema: Schema = {
export const XYSeriesSchema: Schema<XYSeries> = {
seriesId: assertNumber,
xValues: [number], // lossy conversion if too big
yValues: [assertNumber],
Expand Down Expand Up @@ -48,7 +48,7 @@ export interface XYSeries {
tags?: number[];
}

export const XYModelSchema: Schema = {
export const XYModelSchema: Schema<XYModel> = {
series: [XYSeriesSchema],
};

Expand Down
2 changes: 1 addition & 1 deletion tsp-typescript-client/src/protocol/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const number = `number`;
/**
* Throw if `input` is not a `number`.
*/
export const assertNumber: Schema = (num) => {
export const assertNumber: Schema<number> = (num) => {
if (typeof num === bigint) {
throw new TypeError(`Expected ${num} to be ${number}, found ${bigint}!`);
}
Expand Down
1 change: 0 additions & 1 deletion tsp-typescript-client/src/protocol/tsp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export class TspClient {
parameters: Query,
): Promise<TspClientResponse<GenericResponse<XYModel>>> {
const url = this.baseUrl + '/experiments/' + expUUID + '/outputs/XY/' + outputID + '/xy';
console.log(JSON.stringify(GenericResponseSchema(XYModelSchema)));
return RestClient.post(url, parameters, GenericResponseSchema(XYModelSchema));
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6277,10 +6277,10 @@ whatwg-url@^8.0.0, whatwg-url@^8.4.0, whatwg-url@^8.5.0:
tr46 "^2.1.0"
webidl-conversions "^6.1.0"

when-json-met-bigint@^0.21.0:
version "0.21.0"
resolved "https://registry.yarnpkg.com/when-json-met-bigint/-/when-json-met-bigint-0.21.0.tgz#cf2d1e78690a84767b073d8c05f2a419df7117b7"
integrity sha512-RZwbeIBslSCWR7oXT03mV2ojwy5A+iO7k55mNsmqrsWnvUqJtmimv4M9wULd8I9jHZj7HnYlW1ZRaQzj2FRjFg==
when-json-met-bigint@^0.27.0:
version "0.27.0"
resolved "https://registry.yarnpkg.com/when-json-met-bigint/-/when-json-met-bigint-0.27.0.tgz#0d28e02fd8af53c83ccf0f403b85e33698e6f878"
integrity sha512-0YsgwxDNDD0WHZvCm4MCCZtO42584C3onB2YY6ujdP4inaJm3vh7ZZnXIb2hQeDinq+sEfDsVL75Lf1CpxsBow==

which-boxed-primitive@^1.0.2:
version "1.0.2"
Expand Down

0 comments on commit 9adc68e

Please sign in to comment.