Skip to content

Commit

Permalink
finalize ts-codegen v2
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmossas committed Jul 23, 2024
1 parent b7668ce commit 77d2eab
Show file tree
Hide file tree
Showing 16 changed files with 6,771 additions and 12,071 deletions.
5 changes: 4 additions & 1 deletion languages/ts/ts-client/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ test("error messages", async () => {
const request = await arriSafeRequest({
url: "http://thisurldoesntexist.blah",
method: "get",
parser() {},
responseFromJson() {},
responseFromString() {},
serializer() {
return undefined;
},
headers: undefined,
clientVersion: "",
});
expect(!request.success);
if (!request.success) {
Expand Down
5 changes: 3 additions & 2 deletions languages/ts/ts-client/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export interface ArriRequestOpts<
method: HttpMethod;
headers: EventSourcePlusOptions["headers"];
params?: TParams;
parser: (input: string) => TType;
responseFromJson: (input: Record<string, unknown>) => TType;
responseFromString: (input: string) => TType;
serializer: (
input: TParams,
) => TParams extends undefined ? undefined : string;
Expand Down Expand Up @@ -50,7 +51,7 @@ export async function arriRequest<
body,
headers,
});
return opts.parser(result);
return opts.responseFromJson(result);
} catch (err) {
const error = err as any as FetchError;
if (isArriError(error.data)) {
Expand Down
2 changes: 1 addition & 1 deletion languages/ts/ts-client/src/sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function arriSseRequest<
message.event === undefined ||
message.event === ""
) {
options.onMessage?.(opts.parser(message.data));
options.onMessage?.(opts.responseFromString(message.data));
return;
}
if (message.event === "done") {
Expand Down
11 changes: 7 additions & 4 deletions languages/ts/ts-client/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function isBrowser() {
interface WsControllerOptions<TParams, TResponse> {
url: string;
serializer: (input: TParams) => string;
parser: (input: string) => TResponse;
responseFromJson: (input: Record<string, unknown>) => TResponse;
responseFromString: (input: string) => TResponse;
onMessage?: WsMessageHook<TResponse>;
onErrorMessage?: WsErrorHook;
onConnectionError?: WsErrorHook;
Expand All @@ -31,7 +32,8 @@ interface ArriWsRequestOptions<TParams = any, TResponse = any> {
url: string;
headers?: EventSourcePlusOptions["headers"];
params?: TParams;
parser: (input: string) => TResponse;
responseFromJson: (input: Record<string, unknown>) => TResponse;
responseFromString: (input: string) => TResponse;
serializer: (input: TParams) => string;
onMessage?: WsMessageHook<TResponse>;
onError?: WsErrorHook;
Expand Down Expand Up @@ -72,7 +74,8 @@ export async function arriWsRequest<
try {
const controller = new WsController<TParams, TResponse>({
url,
parser: opts.parser,
responseFromJson: opts.responseFromJson,
responseFromString: opts.responseFromString,
serializer: opts.serializer ?? ((_) => ""),
onOpen: opts.onOpen,
onClose: opts.onClose,
Expand Down Expand Up @@ -107,7 +110,7 @@ export class WsController<TParams, TResponse> {
constructor(opts: WsControllerOptions<TParams, TResponse>) {
this.url = opts.url;
this._serializer = opts.serializer;
this._parser = opts.parser;
this._parser = opts.responseFromString;
this.onOpen = opts.onOpen;
this.onClose = opts.onClose;
this.onErrorMessage = opts.onErrorMessage;
Expand Down
88 changes: 88 additions & 0 deletions languages/ts/ts-codegen-reference/src/referenceClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
$$Book,
$$ObjectWithEveryType,
$$ObjectWithNullableFields,
$$ObjectWithOptionalFields,
$$RecursiveObject,
Book,
ObjectWithEveryType,
ObjectWithNullableFields,
ObjectWithOptionalFields,
RecursiveObject,
} from "./referenceClient";

Expand Down Expand Up @@ -93,6 +95,84 @@ describe("ObjectWithEveryType", () => {
});
});

describe("ObjectWithOptionalFields", () => {
const allUndefinedTargetValue: ObjectWithOptionalFields = {
string: undefined,
boolean: undefined,
timestamp: undefined,
float32: undefined,
float64: undefined,
int8: undefined,
uint8: undefined,
int16: undefined,
uint16: undefined,
int32: undefined,
uint32: undefined,
int64: undefined,
uint64: undefined,
enum: undefined,
object: undefined,
array: undefined,
record: undefined,
discriminator: undefined,
any: undefined,
};
const noUndefinedTargetValue: ObjectWithOptionalFields = {
string: "",
boolean: false,
timestamp: testDate,
float32: 1.5,
float64: 1.5,
int8: 1,
uint8: 1,
int16: 10,
uint16: 10,
int32: 100,
uint32: 100,
int64: 1000n,
uint64: 1000n,
enum: "BAZ",
object: {
id: "1",
content: "hello world",
},
array: [true, false, false],
record: {
A: true,
B: false,
},
discriminator: {
typeName: "C",
id: "",
name: "",
date: testDate,
},
any: "hello world",
};
const allUndefinedJson = testFile(
"ObjectWithOptionalFields_AllUndefined.json",
);
const noUndefinedJson = testFile(
"ObjectWithOptionalFields_NoUndefined.json",
);
test("JSON parsing", () => {
expect(
$$ObjectWithOptionalFields.fromJsonString(allUndefinedJson),
).toStrictEqual(allUndefinedTargetValue);
expect(
$$ObjectWithOptionalFields.fromJsonString(noUndefinedJson),
).toStrictEqual(noUndefinedTargetValue);
});
test("JSON output", () => {
expect(
$$ObjectWithOptionalFields.toJsonString(allUndefinedTargetValue),
).toEqual(allUndefinedJson);
expect(
$$ObjectWithOptionalFields.toJsonString(noUndefinedTargetValue),
).toEqual(noUndefinedJson);
});
});

describe("ObjectWithNullableFields", () => {
const allNullTargetValue: ObjectWithNullableFields = {
string: null,
Expand Down Expand Up @@ -161,6 +241,14 @@ describe("ObjectWithNullableFields", () => {
expect(allNullResult).toStrictEqual(allNullTargetValue);
expect(noNullResult).toStrictEqual(noNullTargetValue);
});
test("JSON output", () => {
expect(
$$ObjectWithNullableFields.toJsonString(allNullTargetValue),
).toEqual(allNullJsonReference);
expect(
$$ObjectWithNullableFields.toJsonString(noNullTargetValue),
).toEqual(noNullJsonReference);
});
});

describe("RecursiveObject", () => {
Expand Down
65 changes: 43 additions & 22 deletions languages/ts/ts-codegen-reference/src/referenceClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This file was autogenerated by @arrirpc/codegen-ts. Do not modify directly.
// For more information visit https://github.com/modiimedia/arri

/* eslint-disable */
// @ts-nocheck
import {
Expand All @@ -6,6 +9,7 @@ import {
arriRequest,
arriSseRequest,
arriWsRequest,
type EventSourceController,
INT8_MAX,
INT8_MIN,
INT16_MAX,
Expand All @@ -16,36 +20,42 @@ import {
INT64_MIN,
isObject,
serializeString,
SseOptions,
type SseOptions,
UINT8_MAX,
UINT16_MAX,
UINT32_MAX,
UINT64_MAX,
WsOptions,
type WsController,
type WsOptions,
} from "@arrirpc/client";

type HeaderMap = Record<string, string | undefined>;

export class ExampleClient {
private readonly _baseUrl: string;
private readonly _headers: HeaderMap | (() => HeaderMap);
private readonly _headers:
| HeaderMap
| (() => HeaderMap | Promise<HeaderMap>);
books: ExampleClientBooksService;
constructor(
options: {
baseUrl?: string;
headers?: HeaderMap | (() => HeaderMap);
headers?: HeaderMap | (() => HeaderMap | Promise<HeaderMap>);
} = {},
) {
this._baseUrl = options.baseUrl ?? "";
this._headers = options.headers ?? {};
this.books = new ExampleClientBooksService(options);
}

async sendObject(params: NestedObject) {
async sendObject(params: NestedObject): Promise<NestedObject> {
return arriRequest<NestedObject, NestedObject>({
url: `${this._baseUrl}/send-object`,
method: "post",
headers: this._headers,
params: params,
parser: $$NestedObject.fromJsonString,
responseFromJson: $$NestedObject.fromJson,
responseFromString: $$NestedObject.fromJsonString,
serializer: $$NestedObject.toJsonString,
clientVersion: "20",
});
Expand All @@ -54,57 +64,68 @@ export class ExampleClient {

export class ExampleClientBooksService {
private readonly _baseUrl: string;
private readonly _headers: HeaderMap | (() => HeaderMap);
private readonly _headers:
| HeaderMap
| (() => HeaderMap | Promise<HeaderMap>);
constructor(
options: {
baseUrl?: string;
headers?: HeaderMap | (() => HeaderMap);
headers?: HeaderMap | (() => HeaderMap | Promise<HeaderMap>);
} = {},
) {
this._baseUrl = options.baseUrl ?? "";
this._headers = options.headers ?? {};
}
async getBook(params: BookParams) {
async getBook(params: BookParams): Promise<Book> {
return arriRequest<Book, BookParams>({
url: `${this._baseUrl}/books/get-book`,
method: "get",
headers: this._headers,
params: params,
parser: $$Book.fromJsonString,
responseFromJson: $$Book.fromJson,
responseFromString: $$Book.fromJsonString,
serializer: $$BookParams.toUrlQueryString,
clientVersion: "20",
});
}
async createBook(params: Book) {
async createBook(params: Book): Promise<Book> {
return arriRequest<Book, Book>({
url: `${this._baseUrl}/books/create-book`,
method: "post",
headers: this._headers,
params: params,
parser: $$Book.fromJsonString,
responseFromJson: $$Book.fromJson,
responseFromString: $$Book.fromJsonString,
serializer: $$Book.toJsonString,
clientVersion: "20",
});
}
async watchBook(params: BookParams, options: SseOptions<Book> = {}) {
watchBook(
params: BookParams,
options: SseOptions<Book> = {},
): EventSourceController {
return arriSseRequest<Book, BookParams>(
{
url: `${this._baseUrl}/books/watch-book`,
method: "get",
headers: this._headers,
params: params,
parser: $$Book.fromJsonString,
responseFromJson: $$Book.fromJson,
responseFromString: $$Book.fromJsonString,
serializer: $$BookParams.toUrlQueryString,
clientVersion: "20",
},
options,
);
}
async createConnection(options: WsOptions<Book> = {}) {
async createConnection(
options: WsOptions<Book> = {},
): Promise<WsController<BookParams, Book>> {
return arriWsRequest<BookParams, Book>({
url: `${this._baseUrl}/books/create-connection`,
headers: this._headers,
parser: $$Book.fromJsonString,
responseFromJson: $$Book.fromJson,
responseFromString: $$Book.fromJsonString,
serializer: $$BookParams.toJsonString,
onOpen: options.onOpen,
onClose: options.onClose,
Expand Down Expand Up @@ -625,8 +646,8 @@ export const $$ObjectWithEveryType: ArriModelValidator<ObjectWithEveryType> = {
json += "[";
for (let i = 0; i < input.array.length; i++) {
if (i !== 0) json += ",";
const _element = input.array[i];
json += `${_element}`;
const _inputArrayEl = input.array[i];
json += `${_inputArrayEl}`;
}
json += "]";
json += ',"record":';
Expand Down Expand Up @@ -1393,8 +1414,8 @@ export const $$ObjectWithOptionalFields: ArriModelValidator<ObjectWithOptionalFi
json += "[";
for (let i = 0; i < input.array.length; i++) {
if (i !== 0) json += ",";
const _element = input.array[i];
json += `${_element}`;
const _inputArrayEl = input.array[i];
json += `${_inputArrayEl}`;
}
json += "]";
_hasKey = true;
Expand Down Expand Up @@ -1871,8 +1892,8 @@ export const $$ObjectWithNullableFields: ArriModelValidator<ObjectWithNullableFi
json += "[";
for (let i = 0; i < input.array.length; i++) {
if (i !== 0) json += ",";
const _element = input.array[i];
json += `${_element}`;
const _inputArrayEl = input.array[i];
json += `${_inputArrayEl}`;
}
json += "]";
} else {
Expand Down
Loading

0 comments on commit 77d2eab

Please sign in to comment.