From ca4281b62cb9c33ec6e8f32418b5fb9224189009 Mon Sep 17 00:00:00 2001 From: fmvilas Date: Wed, 30 Oct 2024 18:47:18 +0100 Subject: [PATCH 1/3] fix: imports are not working correctly --- packages/gleequore/package.json | 5 +- packages/gleequore/src/index.d.ts | 92 --------------------------- packages/gleequore/src/index.ts | 91 +++++++++++++++++++++++++- packages/gleequore/src/lib/adapter.ts | 5 +- packages/gleequore/src/lib/message.ts | 4 +- packages/gleequore/{try.js => try.ts} | 4 ++ packages/gleequore/tsconfig.json | 7 +- packages/shared-utils/index.ts | 2 +- 8 files changed, 103 insertions(+), 107 deletions(-) delete mode 100644 packages/gleequore/src/index.d.ts rename packages/gleequore/{try.js => try.ts} (95%) diff --git a/packages/gleequore/package.json b/packages/gleequore/package.json index eaacc0527..e652dffa2 100644 --- a/packages/gleequore/package.json +++ b/packages/gleequore/package.json @@ -8,10 +8,9 @@ "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "files": [ - "dist", - "./src/index.d.ts" + "dist" ], - "types": "./src/index.d.ts", + "types": "./dist/index.d.ts", "scripts": { "build": "tsc", "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION", diff --git a/packages/gleequore/src/index.d.ts b/packages/gleequore/src/index.d.ts deleted file mode 100644 index 808ab4fc1..000000000 --- a/packages/gleequore/src/index.d.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { AsyncAPIDocumentInterface as AsyncAPIDocument } from '@asyncapi/parser' -import GleeQuoreAdapter from './lib/adapter.js' -import GleeQuoreClusterAdapter from './lib/cluster.js' -import GleeQuoreConnection from './lib/connection.js' -import GleeQuore from './index.ts' -import type GleeQuoreMessage from './lib/message.js' - -export { default as GleeQuoreAdapter } from './lib/adapter.js' -export { default as GleeQuoreMessage } from './lib/message.js' -export { default as GleeQuoreConnection } from './lib/connection.js' -export { default as GleeQuoreClusterAdapter } from './lib/cluster.js' -export { default as GleeQuoreError } from './errors.js' - -export interface AuthFunctionInfo { - clientAuth?: GleeQuoreAuthFunction - serverAuth?: GleeQuoreAuthFunction -} - -export type AuthProps = { - getToken: () => string - getUserPass: () => { - username: string - password: string - } - getCert: () => string - getOauthToken: () => string - getHttpAPIKeys: (name: string) => string - getAPIKeys: () => string -} - -export type GleeQuoreClusterAdapterConfig = { - adapter?: string | typeof GleeQuoreClusterAdapter - name?: string - url: string -} - -export type GleeQuoreFunctionEvent = { - request: GleeQuoreMessage - app: GleeQuore - serverName: string - connection?: GleeQuoreConnection - payload?: any - query?: QueryParam - headers?: { [key: string]: string } - channel?: string -} - -export type GleeQuoreLifecycleEvent = Omit - -export type GleeQuoreAuthFunctionEvent = { - app: GleeQuore - authProps: AuthProps - done: any - serverName: string - doc: any -} - -export type GleeQuoreFunction = ( - event: GleeQuoreFunctionEvent -) => Promise - -export type GleeQuoreLifecycleFunction = ( - event: GleeQuoreLifecycleEvent -) => Promise - -export type GleeQuoreAuthFunction = ( - event: GleeQuoreAuthFunctionEvent -) => Promise | void - -export interface GleeQuoreAdapterOptions { - glee: GleeQuore; - serverName: string; - server: ServerInterface; - parsedAsyncAPI: AsyncAPIDocument; - config?: object -} - -export type AdapterRecord = { - Adapter: typeof GleeQuoreAdapter - instance?: GleeQuoreAdapter - serverName: string - server: ServerInterface - asyncapi: AsyncAPIDocumentInterface - config?: object -} - -export type ClusterAdapterRecord = { - Adapter: typeof GleeQuoreClusterAdapter - instance?: GleeQuoreClusterAdapter, - clusterName?: string, - clusterURL?: string -} diff --git a/packages/gleequore/src/index.ts b/packages/gleequore/src/index.ts index d2743ed5a..183f2ea87 100644 --- a/packages/gleequore/src/index.ts +++ b/packages/gleequore/src/index.ts @@ -9,7 +9,7 @@ import GleeQuoreRouter, { ChannelMiddlewareTuple, GenericMiddleware, } from './lib/router.js' -import GleeQuoreMessage, { IGleeQuoreMessageConstructor } from './lib/message.js' +import GleeQuoreMessage, { IGleeQuoreMessageConstructor, QueryParam } from './lib/message.js' import { matchChannel, getParams, getMessagesSchema } from '@asyncapi/glee-shared-utils' import { duplicateMessage } from './lib/utils.js' import GleeQuoreConnection from './lib/connection.js' @@ -20,8 +20,87 @@ import json2string from './middlewares/json2string.js' import validate from './middlewares/validate.js' import existsInAsyncAPI from './middlewares/existsInAsyncAPI.js' import validateConnection from './middlewares/validateConnection.js' -import { AsyncAPIDocumentInterface } from '@asyncapi/parser' -import { AdapterRecord, AuthFunctionInfo, ClusterAdapterRecord, GleeQuoreFunction, GleeQuoreLifecycleFunction, GleeQuoreLifecycleEvent, GleeQuoreFunctionEvent, GleeQuoreAuthFunctionEvent, GleeQuoreAdapterOptions } from './index.d.js' +import type { AsyncAPIDocumentInterface, ServerInterface } from '@asyncapi/parser' + +export interface AuthFunctionInfo { + clientAuth?: GleeQuoreAuthFunction + serverAuth?: GleeQuoreAuthFunction +} + +export type AuthProps = { + getToken: () => string + getUserPass: () => { + username: string + password: string + } + getCert: () => string + getOauthToken: () => string + getHttpAPIKeys: (name: string) => string + getAPIKeys: () => string +} + +export type GleeQuoreClusterAdapterConfig = { + adapter?: string | typeof GleeQuoreClusterAdapter + name?: string + url: string +} + +export type GleeQuoreFunctionEvent = { + request: GleeQuoreMessage + app: GleeQuore + serverName: string + connection?: GleeQuoreConnection + payload?: any + query?: QueryParam + headers?: { [key: string]: string } + channel?: string +} + +export type GleeQuoreLifecycleEvent = Omit + +export type GleeQuoreAuthFunctionEvent = { + app: GleeQuore + authProps: AuthProps + done: any + serverName: string + doc: any +} + +export type GleeQuoreFunction = ( + event: GleeQuoreFunctionEvent +) => Promise | any + +export type GleeQuoreLifecycleFunction = ( + event: GleeQuoreLifecycleEvent +) => Promise | any + +export type GleeQuoreAuthFunction = ( + event: GleeQuoreAuthFunctionEvent +) => Promise | void + +export interface GleeQuoreAdapterOptions { + glee: GleeQuore; + serverName: string; + server: ServerInterface; + parsedAsyncAPI: AsyncAPIDocumentInterface; + config?: object +} + +export type AdapterRecord = { + Adapter: typeof GleeQuoreAdapter + instance?: GleeQuoreAdapter + serverName: string + server: ServerInterface + asyncapi: AsyncAPIDocumentInterface + config?: object +} + +export type ClusterAdapterRecord = { + Adapter: typeof GleeQuoreClusterAdapter + instance?: GleeQuoreClusterAdapter, + clusterName?: string, + clusterURL?: string +} const debug = Debug('gleequore') @@ -641,3 +720,9 @@ export default class GleeQuore { return [...new Set(serverNames)] // Dedupe the array } } + +export { default as GleeQuoreAdapter } from './lib/adapter.js' +export { default as GleeQuoreMessage } from './lib/message.js' +export { default as GleeQuoreConnection } from './lib/connection.js' +export { default as GleeQuoreClusterAdapter } from './lib/cluster.js' +export { default as GleeQuoreError } from './errors.js' \ No newline at end of file diff --git a/packages/gleequore/src/lib/adapter.ts b/packages/gleequore/src/lib/adapter.ts index b1b62f0c2..1b7c6f2f2 100644 --- a/packages/gleequore/src/lib/adapter.ts +++ b/packages/gleequore/src/lib/adapter.ts @@ -6,7 +6,8 @@ import GleeQuoreConnection from './connection.js' import GleeQuore from '../index.js' import GleeQuoreMessage from './message.js' import { resolveFunctions, validateData } from '@asyncapi/glee-shared-utils' -import { AuthProps, GleeQuoreAdapterOptions } from '../index.d.js' +import type { IValidateDataReturn } from '@asyncapi/glee-shared-utils' +import { AuthProps, GleeQuoreAdapterOptions } from '../index.js' import GleeQuoreError from '../errors.js' export type EnrichedEvent = { @@ -258,7 +259,7 @@ class GleeQuoreAdapter extends EventEmitter { throw new Error('Method `send` is not implemented.') } - validate(data: any, schema: object, triggerError = false) { + validate(data: any, schema: object, triggerError = false): IValidateDataReturn { const { isValid, errors, humanReadableError } = validateData(data, schema) if (!isValid && triggerError) { throw new GleeQuoreError({ humanReadableError, errors }) diff --git a/packages/gleequore/src/lib/message.ts b/packages/gleequore/src/lib/message.ts index ea0b78c63..059aa7b87 100644 --- a/packages/gleequore/src/lib/message.ts +++ b/packages/gleequore/src/lib/message.ts @@ -2,8 +2,8 @@ import EventEmitter from 'events' import GleeQuoreConnection from './connection.js' import { OperationInterface } from '@asyncapi/parser' -type MessageHeaders = { [key: string]: any } -type QueryParam = { [key: string]: string } | { [key: string]: string[] } +export type MessageHeaders = { [key: string]: any } +export type QueryParam = { [key: string]: string } | { [key: string]: string[] } export interface IGleeQuoreMessageConstructor { payload?: any diff --git a/packages/gleequore/try.js b/packages/gleequore/try.ts similarity index 95% rename from packages/gleequore/try.js rename to packages/gleequore/try.ts index f9373b2c9..e3dede196 100644 --- a/packages/gleequore/try.js +++ b/packages/gleequore/try.ts @@ -39,6 +39,10 @@ const { document: asyncapi } = await parser.parse({ }, }) +if (!asyncapi) { + throw new Error('Not a valid AsyncAPI document') +} + const gleeQuore = new GleeQuore(asyncapi) // gleeQuore.addAdapter(HttpAdapter, 'test', { diff --git a/packages/gleequore/tsconfig.json b/packages/gleequore/tsconfig.json index 9e5eb69c8..c5fc10ea2 100644 --- a/packages/gleequore/tsconfig.json +++ b/packages/gleequore/tsconfig.json @@ -7,11 +7,10 @@ "esModuleInterop": true, "moduleResolution": "nodenext", "module": "NodeNext", - "rootDir": "./src" + "rootDir": "./src", + "declaration": true }, - "include": [ - "./src/**/*" - ], + "include": ["./src/**/*"], "exclude": [ "node_modules", "lib", diff --git a/packages/shared-utils/index.ts b/packages/shared-utils/index.ts index b854b95b8..a90b1d454 100644 --- a/packages/shared-utils/index.ts +++ b/packages/shared-utils/index.ts @@ -3,7 +3,7 @@ import Ajv from 'ajv' import betterAjvErrors from 'better-ajv-errors' import { pathToRegexp } from 'path-to-regexp' -interface IValidateDataReturn { +export interface IValidateDataReturn { errors?: void | betterAjvErrors.IOutputError[] humanReadableError?: void | betterAjvErrors.IOutputError[] isValid: boolean | PromiseLike From b6c9a886e5d93da0ee585323514bd03f3df35653 Mon Sep 17 00:00:00 2001 From: fmvilas Date: Wed, 30 Oct 2024 19:01:45 +0100 Subject: [PATCH 2/3] add changeset --- .changeset/green-beds-dress.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/green-beds-dress.md diff --git a/.changeset/green-beds-dress.md b/.changeset/green-beds-dress.md new file mode 100644 index 000000000..ed611f68c --- /dev/null +++ b/.changeset/green-beds-dress.md @@ -0,0 +1,6 @@ +--- +"@asyncapi/glee-shared-utils": patch +"@asyncapi/gleequore": patch +--- + +Importing gleequore doesn't work correctly. It imports the types but doesn't let you import the implementation so it's unusable. This is fixed in this release. From 19d752ffb2e77d93b11a86ea2925d4fa8fcad9aa Mon Sep 17 00:00:00 2001 From: fmvilas Date: Wed, 30 Oct 2024 19:08:09 +0100 Subject: [PATCH 3/3] fix tests --- packages/gleequore/jest.config.js | 13 ++++++++++++- packages/gleequore/test/index.test.ts | 3 --- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/gleequore/jest.config.js b/packages/gleequore/jest.config.js index 4e783dd58..aeeb86075 100644 --- a/packages/gleequore/jest.config.js +++ b/packages/gleequore/jest.config.js @@ -14,6 +14,17 @@ export default { '^nimma/legacy$': '/../../node_modules/nimma/dist/legacy/cjs/index.js', '^nimma/fallbacks$': '/../../node_modules/nimma/dist/legacy/cjs/fallbacks/index.js', + '^@asyncapi/glee-shared-utils$': '/../../packages/shared-utils/index.ts' }, - transform: {}, + transform: { + '^.+\\.tsx?$': ['ts-jest', { + useESM: true, + tsconfig: { + rootDir: "../../", + baseUrl: ".", + } + }] + }, + roots: ['/src/', '/test/', '/../../packages/shared-utils/'], + modulePaths: ['/src/', '/../../packages/shared-utils/'], } diff --git a/packages/gleequore/test/index.test.ts b/packages/gleequore/test/index.test.ts index b16460d46..0ca310dfb 100644 --- a/packages/gleequore/test/index.test.ts +++ b/packages/gleequore/test/index.test.ts @@ -1,10 +1,7 @@ import { describe, it, jest, beforeEach, expect } from '@jest/globals'; import { AsyncAPIDocumentInterface } from '@asyncapi/parser'; import GleeQuore from '../src/index'; -import { EventEmitter } from 'events'; import { Parser } from '@asyncapi/parser' -import { GleeQuoreAuthFunction, GleeQuoreAuthFunctionEvent } from '../src/index.d'; -import GleeQuoreConnection from '../src/lib/connection'; const asyncapiDocumentAsJS = { asyncapi: '3.0.0',