From 0ddddd1f45977c72f12754435ce8a7481eb74b2e Mon Sep 17 00:00:00 2001 From: Kostiantyn Smyrnov Date: Tue, 4 Apr 2023 15:57:49 +0200 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Updated=20messages?= =?UTF-8?q?=20schemes=20and=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/client/src/App.tsx | 6 +- src/client/index.ts | 8 +-- src/client/requestsRegistry.ts | 53 ++++++++-------- src/node/index.ts | 2 +- src/node/requestManager.ts | 2 +- src/shared/messages.ts | 107 +++++++++++++++------------------ 6 files changed, 86 insertions(+), 92 deletions(-) diff --git a/examples/client/src/App.tsx b/examples/client/src/App.tsx index 6e817cde..bb949319 100644 --- a/examples/client/src/App.tsx +++ b/examples/client/src/App.tsx @@ -16,6 +16,8 @@ const defaultExpire = '30s'; /** Default topic to publish requests the same as for the supplier node */ const defaultTopic = 'hello'; +type RequestsRegistryRecord = Required>; + interface FormValues { topic: string; message: string; @@ -27,7 +29,7 @@ interface RequestFormProps { } interface RequestsProps { - requests: Required>[]; + requests: RequestsRegistryRecord[]; subscribed?: (id: string) => boolean; onClear(): void; onCancel(id: string): void; @@ -147,7 +149,7 @@ export const Requests = ({ requests, subscribed, onClear, onCancel }: RequestsPr export const App = () => { const client = useRef | undefined>(); const [connected, setConnected] = useState(false); - const [requests, setRequests] = useState>[]>( + const [requests, setRequests] = useState( [], ); const [error, setError] = useState(); diff --git a/src/client/index.ts b/src/client/index.ts index ce44f7dd..5516b4e1 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -329,10 +329,10 @@ export class Client< } /** Check is the message is an offer */ - const offer = createOfferDataSchema< - z.ZodType, - z.ZodType - >(this.querySchema, this.offerOptionsSchema).parse(JSON.parse(decodeText(detail.data))); + const offer = createOfferDataSchema( + this.querySchema, + this.offerOptionsSchema, + ).parse(JSON.parse(decodeText(detail.data))); logger.trace('Offer received:', offer); // Verify the offer diff --git a/src/client/requestsRegistry.ts b/src/client/requestsRegistry.ts index 59b3fad7..d2495b7f 100644 --- a/src/client/requestsRegistry.ts +++ b/src/client/requestsRegistry.ts @@ -19,24 +19,29 @@ const logger = createLogger('RequestsRegistry'); /** * Creates request record schema * - * @param {z.ZodType} querySchema Custom request query schema - * @param {z.ZodType} offerOptionsSchema + * @template {CustomRequestQuery} + * @template {CustomOfferOptions} + * @param {z.ZodType} querySchema Custom request query schema + * @param {z.ZodType} offerOptionsSchema * @returns {z.ZodType} Request record schema */ export const createRequestRecordSchema = < - TQuery extends z.ZodTypeAny, - TOfferOptions extends z.ZodTypeAny, + CustomRequestQuery extends GenericQuery, + CustomOfferOptions extends GenericOfferOptions, >( - querySchema: TQuery, - offerOptionsSchema: TOfferOptions, + querySchema: z.ZodType, + offerOptionsSchema: z.ZodType, ) => z .object({ /** Raw request data */ - data: createRequestDataSchema(querySchema), + data: createRequestDataSchema(querySchema), /** Offers associated with a request*/ offers: z.array( - createOfferDataSchema(querySchema, offerOptionsSchema), + createOfferDataSchema( + querySchema, + offerOptionsSchema, + ), ), /** Request cancelation flag */ cancelled: z.boolean().default(false), @@ -84,11 +89,7 @@ export type RequestsRegistryOptions< export type RequestRecord< CustomRequestQuery extends GenericQuery, CustomOfferOptions extends GenericOfferOptions, -> = z.infer< - ReturnType< - typeof createRequestRecordSchema, z.ZodType> - > ->; +> = z.infer>>; /** * Request manager events interface @@ -260,10 +261,10 @@ export class RequestsRegistry< for (let requestRecord of rawRecords) { try { - requestRecord = createRequestRecordSchema< - z.ZodType, - z.ZodType - >(this.client.querySchema, this.client.offerOptionsSchema).parse(requestRecord); + requestRecord = createRequestRecordSchema( + this.client.querySchema, + this.client.offerOptionsSchema, + ).parse(requestRecord); // `record.data` marked as optional because of Zod generics issue this.requests.set( @@ -379,13 +380,11 @@ export class RequestsRegistry< throw new Error('Client not connected to the coordination server yet'); } - request = createRequestDataSchema>(this.client.querySchema).parse( - request, - ); - const requestRecord = createRequestRecordSchema< - z.ZodType, - z.ZodType - >(this.client.querySchema, this.client.offerOptionsSchema).parse({ + request = createRequestDataSchema(this.client.querySchema).parse(request); + const requestRecord = createRequestRecordSchema( + this.client.querySchema, + this.client.offerOptionsSchema, + ).parse({ data: request, offers: [], }); @@ -452,7 +451,7 @@ export class RequestsRegistry< this.requests.set( id, - createRequestRecordSchema, z.ZodType>( + createRequestRecordSchema( this.client.querySchema, this.client.offerOptionsSchema, ).parse(record), @@ -512,7 +511,7 @@ export class RequestsRegistry< * @memberof RequestsRegistry */ addOffer(offer: OfferData) { - offer = createOfferDataSchema, z.ZodType>( + offer = createOfferDataSchema( this.client.querySchema, this.client.offerOptionsSchema, ).parse(offer); @@ -529,7 +528,7 @@ export class RequestsRegistry< this.requests.set( requestId, - createRequestRecordSchema, z.ZodType>( + createRequestRecordSchema( this.client.querySchema, this.client.offerOptionsSchema, ).parse({ diff --git a/src/node/index.ts b/src/node/index.ts index c043b13c..1e3efd6b 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -249,7 +249,7 @@ export class Node< } const offer = await buildOffer( - createBuildOfferOptions, z.ZodType>( + createBuildOfferOptions( this.querySchema, this.offerOptionsSchema, ).parse({ diff --git a/src/node/requestManager.ts b/src/node/requestManager.ts index 69803bc7..5a115283 100644 --- a/src/node/requestManager.ts +++ b/src/node/requestManager.ts @@ -45,7 +45,7 @@ export class RequestManager extends Eve } add(topic: string, data: string) { - const requestData = createRequestDataSchema(this.querySchema).parse( + const requestData = createRequestDataSchema(this.querySchema).parse( JSON.parse(data), ); diff --git a/src/shared/messages.ts b/src/shared/messages.ts index 1a264a2d..9fe38f47 100644 --- a/src/shared/messages.ts +++ b/src/shared/messages.ts @@ -36,10 +36,13 @@ export type GenericQuery = z.infer; /** * Creates request data structure schema * - * @param {z.ZodType} querySchema + * @template {CustomRequestQuery} + * @param {z.ZodType} querySchema * @returns {z.ZodType} */ -export const createRequestDataSchema = (querySchema: T) => +export const createRequestDataSchema = ( + querySchema: z.ZodType, +) => GenericMessageSchema.extend({ /** Request topic */ topic: z.string(), @@ -51,16 +54,19 @@ export const createRequestDataSchema = (querySchema: T) * Request data type */ export type RequestData = z.infer< - ReturnType>> + ReturnType> >; /** * Creates schema for buildRequest method options * - * @param {z.ZodType} querySchema + * @template {CustomRequestQuery} + * @param {z.ZodType} querySchema * @returns {z.ZodType} */ -export const createBuildRequestOptions = (querySchema: T) => +export const createBuildRequestOptions = ( + querySchema: z.ZodType, +) => z .object({ /** Expiration time */ @@ -82,23 +88,25 @@ export const createBuildRequestOptions = (querySchema: T * buildRequest method options type */ export type BuildRequestOptions = z.infer< - ReturnType>> + ReturnType> >; /** * Builds a request * - * @template CustomRequestQuery + * @template {CustomRequestQuery} * @param {BuildRequestOptions} requestOptions * @returns */ export const buildRequest = async ( requestOptions: BuildRequestOptions, ) => { - const { expire, nonce, topic, query, querySchema, idOverride } = createBuildRequestOptions< - typeof requestOptions.querySchema - >(requestOptions.querySchema).parse(requestOptions); - const request = createRequestDataSchema(querySchema); + const { expire, nonce, topic, query, idOverride } = createBuildRequestOptions( + requestOptions.querySchema as z.ZodType, + ).parse(requestOptions); + const request = createRequestDataSchema( + requestOptions.querySchema as z.ZodType, + ); return (await request.parseAsync({ id: idOverride ?? uuid4(), expire: typeof expire === 'number' ? parseSeconds(expire) : nowSec() + parseSeconds(expire), @@ -178,21 +186,21 @@ export type GenericOfferOptions = z.infer; /** * Creates a final offer data schema * - * @template TQuery - * @template TOfferOptions - * @param {TQuery} querySchema - * @param {TOfferOptions} offerOptionsSchema + * @template {CustomRequestQuery} + * @template {CustomOfferOptions} + * @param {z.ZodType} querySchema + * @param {z.ZodType} offerOptionsSchema */ export const createOfferDataSchema = < - TQuery extends z.ZodTypeAny, - TOfferOptions extends z.ZodTypeAny, + CustomRequestQuery extends GenericQuery, + CustomOfferOptions extends GenericOfferOptions, >( - querySchema: TQuery, - offerOptionsSchema: TOfferOptions, + querySchema: z.ZodType, + offerOptionsSchema: z.ZodType, ) => GenericMessageSchema.extend({ /** Copy of request */ - request: createRequestDataSchema(querySchema), + request: createRequestDataSchema(querySchema), /** Offer options */ options: offerOptionsSchema, /** Payment options */ @@ -211,11 +219,7 @@ export const createOfferDataSchema = < export type OfferData< CustomRequestQuery extends GenericQuery, CustomOfferOptions extends GenericOfferOptions, -> = z.infer< - ReturnType< - typeof createOfferDataSchema, z.ZodType> - > ->; +> = z.infer>>; /** * EIP-712 JSON schema types for offer @@ -260,17 +264,17 @@ export const offerEip712Types: Record> = { /** * Creates a schema for `buildOffer` method options * - * @template TQuery - * @template TOfferOptions - * @param {TQuery} querySchema - * @param {TOfferOptions} offerOptionsSchema + * @template {CustomRequestQuery} + * @template {CustomOfferOptions} + * @param {z.ZodType} querySchema + * @param {z.ZodType} offerOptionsSchema */ export const createBuildOfferOptions = < - TQuery extends z.ZodTypeAny, - TOfferOptions extends z.ZodTypeAny, + CustomRequestQuery extends GenericQuery, + CustomOfferOptions extends GenericOfferOptions, >( - querySchema: TQuery, - offerOptionsSchema: TOfferOptions, + querySchema: z.ZodType, + offerOptionsSchema: z.ZodType, ) => z .object({ @@ -280,7 +284,7 @@ export const createBuildOfferOptions = < optionsSchema: z.instanceof(z.ZodType), supplierId: z.string(), expire: z.string().or(z.number()), - request: createRequestDataSchema(querySchema), + request: createRequestDataSchema(querySchema), options: offerOptionsSchema, payment: z.array(PaymentOptionSchema), cancel: z.array(CancelOptionSchema), @@ -293,26 +297,17 @@ export const createBuildOfferOptions = < /** * Type for `buildOffer` method options - * - * @template TQuery - * @template TOfferOptions - * @param {TQuery} querySchema - * @param {TOfferOptions} offerOptionsSchema */ export type BuildOfferOptions< CustomRequestQuery extends GenericQuery, CustomOfferOptions extends GenericOfferOptions, -> = z.infer< - ReturnType< - typeof createBuildOfferOptions, z.ZodType> - > ->; +> = z.infer>>; /** * Builds an offer * - * @template CustomRequestQuery - * @template CustomOfferOptions + * @template {CustomRequestQuery} + * @template {CustomOfferOptions} * @param {BuildOfferOptions} offerOptions * @returns */ @@ -333,13 +328,11 @@ export const buildOffer = async < transferable, signer, signatureOverride, - querySchema, - optionsSchema, idOverride, expire, - } = createBuildOfferOptions( - offerOptions.querySchema, - offerOptions.optionsSchema, + } = createBuildOfferOptions( + offerOptions.querySchema as z.ZodType, + offerOptions.optionsSchema as z.ZodType, ).parse(offerOptions); const unsignedOfferPayload = UnsignedOfferPayloadSchema.parse({ @@ -372,10 +365,10 @@ export const buildOffer = async < throw new Error('Either signer or signatureOverride must be provided'); } - const offerSchema = createOfferDataSchema< - typeof offerOptions.querySchema, - typeof offerOptions.optionsSchema - >(querySchema, optionsSchema); + const offerSchema = createOfferDataSchema( + offerOptions.querySchema as z.ZodType, + offerOptions.optionsSchema as z.ZodType, + ); return (await offerSchema.parseAsync({ id: idOverride ?? uuid4(), @@ -393,8 +386,8 @@ export const buildOffer = async < /** * Verifies signed offer * - * @template CustomRequestQuery - * @template CustomOfferOptions + * @template {CustomRequestQuery} + * @template {CustomOfferOptions} * @param {ContractConfig} contract * @param {string} supplierAddress * @param {OfferData} offer From 71a734a4040570a01c72a8fe67255badccad01f9 Mon Sep 17 00:00:00 2001 From: Kostiantyn Smyrnov Date: Tue, 4 Apr 2023 15:58:29 +0200 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=F0=9F=A4=96=20Updated=20module=20?= =?UTF-8?q?build=20configuration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/client/package.json | 2 +- examples/client/yarn.lock | 8 ++++---- package.json | 4 ++-- tsconfig.json | 2 -- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/client/package.json b/examples/client/package.json index 9500eab0..f02914fb 100644 --- a/examples/client/package.json +++ b/examples/client/package.json @@ -19,7 +19,7 @@ "typescript": "^5.0.3", "vite": "^4.2.1", "zod": "^3.21.4", - "@windingtree/sdk": "0.1.0-beta.9" + "@windingtree/sdk": "0.1.0-beta.10" }, "eslintConfig": { "extends": [ diff --git a/examples/client/yarn.lock b/examples/client/yarn.lock index ca1edae1..73e7e7a3 100644 --- a/examples/client/yarn.lock +++ b/examples/client/yarn.lock @@ -2177,10 +2177,10 @@ magic-string "^0.27.0" react-refresh "^0.14.0" -"@windingtree/sdk@0.1.0-beta.9": - version "0.1.0-beta.9" - resolved "https://registry.yarnpkg.com/@windingtree/sdk/-/sdk-0.1.0-beta.9.tgz#215fe4729d0bb7b777de449d9e3d4ce56241b432" - integrity sha512-L2RuVtCGcCuSAEcUjE25k2BaDx3SbZQJos/ncU9ncmilKFNuPbt+QAmxgveZVZRXCb5fInJyWa+V9bD9NvPAaw== +"@windingtree/sdk@0.1.0-beta.10": + version "0.1.0-beta.10" + resolved "https://registry.yarnpkg.com/@windingtree/sdk/-/sdk-0.1.0-beta.10.tgz#f2092903077a597ffb3c46a6af1fda25b8586bea" + integrity sha512-9CsUr+njuL71L0eHX9GmNuaTgauGHqJzsky/annmW7WDa/GJ5cFI6mowGSMRHeCIAhtOZEtMQUAlWrHRgk1TBQ== dependencies: "@chainsafe/libp2p-gossipsub" "^6.2.0" "@chainsafe/libp2p-noise" "^11.0.4" diff --git a/package.json b/package.json index c1dbc5f4..e87fa452 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,8 @@ ], "license": "MIT", "type": "module", - "types": "./lib/src/index.d.ts", - "main": "./lib/src/index.js", + "types": "./lib/index.d.ts", + "main": "./lib/index.js", "files": [ "lib/**/*", "!**/*.tsbuildinfo" diff --git a/tsconfig.json b/tsconfig.json index 6c316371..24777913 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,8 +14,6 @@ "allowSyntheticDefaultImports": true, "resolveJsonModule": true, "noErrorTruncation": true, - "incremental": true, - "composite": true, "forceConsistentCasingInFileNames": true, "useUnknownInCatchVariables": true, "importsNotUsedAsValues": "remove",