Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
feat: align naming on functions (#37)
Browse files Browse the repository at this point in the history
- Align the naming of functions
- Only use the methods needed in example
  • Loading branch information
KATT authored Oct 6, 2023
1 parent efb29e6 commit 84faf3b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 16 deletions.
7 changes: 5 additions & 2 deletions examples/async/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createTsonParseAsync } from "tupleson";
import waitPort from "wait-port";

import type { ResponseShape } from "./server.js";

import { mapIterable, readableStreamToAsyncIterable } from "./iteratorUtils.js";
import { tsonAsync } from "./shared.js";
import { tsonOptions } from "./shared.js";

const tsonParseAsync = createTsonParseAsync(tsonOptions);

async function main() {
// do a streamed fetch request
Expand All @@ -25,7 +28,7 @@ async function main() {
);

// ✨ ✨ ✨ ✨ parse the response body stream ✨ ✨ ✨ ✨ ✨
const output = await tsonAsync.parse<ResponseShape>(stringIterator);
const output = await tsonParseAsync<ResponseShape>(stringIterator);

// we can now use the output as a normal object
console.log({ output });
Expand Down
7 changes: 5 additions & 2 deletions examples/async/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import http from "node:http";
import { createTsonStringifyAsync } from "tupleson";

import { tsonAsync } from "./shared.js";
import { tsonOptions } from "./shared.js";

const tsonStringifyAsync = createTsonStringifyAsync(tsonOptions);

const randomNumber = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min);
Expand Down Expand Up @@ -50,7 +53,7 @@ async function handleRequest(

const obj = getResponseShape();

for await (const chunk of tsonAsync.stringify(obj)) {
for await (const chunk of tsonStringifyAsync(obj)) {
res.write(chunk);
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/async/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
createTsonAsync,
TsonAsyncOptions,
tsonAsyncIterator,
tsonBigint,
tsonPromise,
} from "tupleson";

export const tsonAsync = createTsonAsync({
export const tsonOptions: TsonAsyncOptions = {
types: [tsonPromise, tsonAsyncIterator, tsonBigint],
});
};
4 changes: 2 additions & 2 deletions src/async/createTsonAsync.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TsonAsyncOptions } from "./asyncTypes.js";
import { createTsonParseAsync } from "./deserializeAsync.js";
import { createAsyncTsonStringify } from "./serializeAsync.js";
import { createTsonStringifyAsync } from "./serializeAsync.js";

export const createTsonAsync = (opts: TsonAsyncOptions) => ({
parse: createTsonParseAsync(opts),
stringify: createAsyncTsonStringify(opts),
stringify: createTsonStringifyAsync(opts),
});
4 changes: 2 additions & 2 deletions src/async/serializeAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, test } from "vitest";
import { tsonAsyncIterator, tsonBigint, tsonPromise } from "../index.js";
import {
createAsyncTsonSerialize,
createAsyncTsonStringify,
createTsonStringifyAsync,
} from "./serializeAsync.js";

test("serialize promise", async () => {
Expand Down Expand Up @@ -157,7 +157,7 @@ test("serialize async iterable", async () => {
});

test("stringify async iterable + promise", async () => {
const stringify = createAsyncTsonStringify({
const stringify = createTsonStringifyAsync({
nonce: () => "__tson",
types: [tsonAsyncIterator, tsonPromise, tsonBigint],
});
Expand Down
2 changes: 1 addition & 1 deletion src/async/serializeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export function createAsyncTsonSerialize(
};
}

export function createAsyncTsonStringify(
export function createTsonStringifyAsync(
opts: TsonAsyncOptions,
): TsonAsyncStringifier {
const indent = (length: number) => " ".repeat(length);
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/tsonPromise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import {
TsonAsyncValueTuple,
createAsyncTsonSerialize,
createAsyncTsonStringify,
createTsonStringifyAsync,
} from "../async/serializeAsync.js";
import { createTsonAsync, tsonPromise } from "../index.js";
import {
Expand Down Expand Up @@ -470,7 +470,7 @@ test("does not crash node when it receives a promise rejection", async () => {
nonce: () => "__tson",
types: [tsonPromise],
};
const stringify = createAsyncTsonStringify(opts);
const stringify = createTsonStringifyAsync(opts);

const parse = createTsonParseAsyncInner(opts);

Expand Down Expand Up @@ -510,7 +510,7 @@ test("stringify promise rejection", async () => {
nonce: () => "__tson",
types: [tsonPromise, tsonError],
};
const stringify = createAsyncTsonStringify(opts);
const stringify = createTsonStringifyAsync(opts);

const parse = createTsonParseAsync(opts);

Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// async
export { createTsonAsync } from "./async/createTsonAsync.js";
export { createAsyncTsonStringify } from "./async/serializeAsync.js";
export { createTsonParseAsync } from "./async/deserializeAsync.js";
export { createTsonStringifyAsync } from "./async/serializeAsync.js";

// sync
export { createTson } from "./sync/createTson.js";
Expand All @@ -9,5 +10,6 @@ export { createTsonSerialize, createTsonStringify } from "./sync/serialize.js";
export * from "./handlers/index.js";

// types
export type { TsonAsyncOptions } from "./async/asyncTypes.js";
export type { TsonType } from "./types.js";
export type { TsonOptions } from "./types.js";

0 comments on commit 84faf3b

Please sign in to comment.