Skip to content

Commit

Permalink
test: expose test component (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoArregui authored Jul 22, 2022
1 parent 95c40bf commit 34395ec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A port used to communicate with [NATS](https://nats.io/), the cloud native messa

```typescript
// src/components.ts
await createNatsComponent({ config, logs }
await createNatsComponent({ config, logs })
```

### Start
Expand Down
3 changes: 3 additions & 0 deletions etc/nats-component.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { JSONCodec } from 'nats';
// Warning: (ae-forgotten-export) The symbol "natsComponent" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "INatsComponent" needs to be exported by the entry point index.d.ts
//
// @public
export function createLocalNatsComponent(components: natsComponent.NeededComponents): Promise<INatsComponent & IBaseComponent>;

// @public
export function createNatsComponent(components: natsComponent.NeededComponents): Promise<INatsComponent & IBaseComponent>;

Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { connect, NatsConnection, JSONCodec } from "nats"
import mitt from "mitt"
import { natsComponent, INatsComponent, NatsEvents, Subscription } from "./types"

export { createLocalNatsComponent } from './test-component'
/**
* Encode/Decode JSON objects into Uint8Array and viceversa
* @public
Expand All @@ -23,7 +24,6 @@ export async function createNatsComponent(

// config
const natsUrl = (await config.getString("NATS_URL")) || "localhost:4222"
const natsConfig = { servers: `${natsUrl}` }
let natsConnection: NatsConnection

const events = mitt<NatsEvents>()
Expand All @@ -38,8 +38,8 @@ export async function createNatsComponent(
.then(() => {
logger.info(`subscription closed for ${topic}`)
})
.catch((err) => {
logger.error(`subscription closed with an error ${err.message}`)
.catch((err: any) => {
logger.error(`subscription closed with an error ${err.toString()}`)
})
return {
unsubscribe: () => sub.unsubscribe(),
Expand All @@ -49,7 +49,7 @@ export async function createNatsComponent(

async function start() {
try {
natsConnection = await connect(natsConfig)
natsConnection = await connect({ servers: `${natsUrl}` })
events.emit("connected")
logger.info(`Connected to NATS: ${natsUrl}`)
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions src/test-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ type PushableChannel = {
push: (value: NatsMsg, resolve: (err?: any) => void) => void
}

/**
* Create a local NATS component, for testing purposes
* @public
*/
export async function createLocalNatsComponent(
components: natsComponent.NeededComponents
): Promise<INatsComponent & IBaseComponent> {
Expand Down

0 comments on commit 34395ec

Please sign in to comment.