From 84faf3bc890cbfc1208fe3ea4212c5629fc88426 Mon Sep 17 00:00:00 2001 From: Alex / KATT Date: Fri, 6 Oct 2023 13:40:32 +0200 Subject: [PATCH] feat: align naming on functions (#37) - Align the naming of functions - Only use the methods needed in example --- examples/async/src/client.ts | 7 +++++-- examples/async/src/server.ts | 7 +++++-- examples/async/src/shared.ts | 6 +++--- src/async/createTsonAsync.ts | 4 ++-- src/async/serializeAsync.test.ts | 4 ++-- src/async/serializeAsync.ts | 2 +- src/handlers/tsonPromise.test.ts | 6 +++--- src/index.ts | 4 +++- 8 files changed, 24 insertions(+), 16 deletions(-) diff --git a/examples/async/src/client.ts b/examples/async/src/client.ts index e09a4a32..734865c1 100644 --- a/examples/async/src/client.ts +++ b/examples/async/src/client.ts @@ -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 @@ -25,7 +28,7 @@ async function main() { ); // ✨ ✨ ✨ ✨ parse the response body stream ✨ ✨ ✨ ✨ ✨ - const output = await tsonAsync.parse(stringIterator); + const output = await tsonParseAsync(stringIterator); // we can now use the output as a normal object console.log({ output }); diff --git a/examples/async/src/server.ts b/examples/async/src/server.ts index 728d1ecf..a892d53b 100644 --- a/examples/async/src/server.ts +++ b/examples/async/src/server.ts @@ -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); @@ -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); } } diff --git a/examples/async/src/shared.ts b/examples/async/src/shared.ts index 3fa83f2e..14f56b45 100644 --- a/examples/async/src/shared.ts +++ b/examples/async/src/shared.ts @@ -1,10 +1,10 @@ import { - createTsonAsync, + TsonAsyncOptions, tsonAsyncIterator, tsonBigint, tsonPromise, } from "tupleson"; -export const tsonAsync = createTsonAsync({ +export const tsonOptions: TsonAsyncOptions = { types: [tsonPromise, tsonAsyncIterator, tsonBigint], -}); +}; diff --git a/src/async/createTsonAsync.ts b/src/async/createTsonAsync.ts index 1e1f4ef1..6bbd33ad 100644 --- a/src/async/createTsonAsync.ts +++ b/src/async/createTsonAsync.ts @@ -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), }); diff --git a/src/async/serializeAsync.test.ts b/src/async/serializeAsync.test.ts index 35d97cc4..2d7e8430 100644 --- a/src/async/serializeAsync.test.ts +++ b/src/async/serializeAsync.test.ts @@ -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 () => { @@ -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], }); diff --git a/src/async/serializeAsync.ts b/src/async/serializeAsync.ts index 02a21539..29bf8543 100644 --- a/src/async/serializeAsync.ts +++ b/src/async/serializeAsync.ts @@ -209,7 +209,7 @@ export function createAsyncTsonSerialize( }; } -export function createAsyncTsonStringify( +export function createTsonStringifyAsync( opts: TsonAsyncOptions, ): TsonAsyncStringifier { const indent = (length: number) => " ".repeat(length); diff --git a/src/handlers/tsonPromise.test.ts b/src/handlers/tsonPromise.test.ts index 80b54d82..d7ae1db6 100644 --- a/src/handlers/tsonPromise.test.ts +++ b/src/handlers/tsonPromise.test.ts @@ -8,7 +8,7 @@ import { import { TsonAsyncValueTuple, createAsyncTsonSerialize, - createAsyncTsonStringify, + createTsonStringifyAsync, } from "../async/serializeAsync.js"; import { createTsonAsync, tsonPromise } from "../index.js"; import { @@ -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); @@ -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); diff --git a/src/index.ts b/src/index.ts index c7f3e43a..2c4975c7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; @@ -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";