1.2.5 • Published 1 year ago

apex-ws v1.2.5

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

APEX WS

Installation

npm i apex-ws

Usage

import { ApexWebSocket } from 'apex-ws'

const endpoints = ['AP function name'] as const

// type for using in class
export type AlphaPoint = Record<
    (typeof endpoints)[number],
    Awaited<ReturnType<ApexWebSocket['getClient']>>[string]
>

const apexWebSocket = new ApexWebSocket({
            url: config.alphaPoint.alphaPointWebsocketURL,
            credentials: {
                username: config.alphaPoint.alphaPointUsername,
                password: config.alphaPoint.alphaPointPassword,
            },
            endpoints: endpoints,
        })
// if using value variable need making it as const like define above
const client = await apexWebSocket.getClient(endpoints)
// OR can pass array directly to making autocomplete of endpoint work
const client = await apexWebSocket.getClient(['functionName1', 'functionName2'])

// params will be object send with request
try {
    const result = await client.functionName1(params)
} catch (error) {
    // handle reject promise here for error
    console.error(error)
    // rethrow error if need
    throw error
}

Options

export interface ApexWebSocketOptions {
    /**
     * AP websocket server url
     */
    url: string
    /**
     * Custom function to run when the websocket connected
     * @param value
     * @returns
     */
    onOpen?: (value?: Event) => void
    /**
     * Custom function to run when the websocket closed
     * @param value
     * @returns
     */
    onClose?: (value?: Event) => void
    /**
     * Username and Password of AP account
     */
    credentials: {
        username: string
        password: string
    }
    /**
     * @default false
     * Log more information in the send seq, function name, and payload
     */
    debugMode?: boolean

    /**
     * @default 1000
     * Delay before retry to create connection in millisecond
     */
    delayBeforeRetryConnect?: number
    /**
     * @default 'fixed'
     * Delay scale up per attempts for linear type
     */
    delayTypeBeforeRetryConnect?: 'fixed' | 'liner'
    /**
     * @default 30000
     * Max delay in millisecond
     */
    maxDelayTimeBeforeRetryConnect?: number
    /**
     * @default false
     * Add 2 space of JSON stringify to print debugMode log
     * now have problem with JSON.string in my custom log so didn't need this for a while
     */
    prettyPrint?: boolean

    /**
     * @default 10000
     * Request timeout in millisecond
     */
    requestTimeout?: number

    /**
     * Custom logger like nestjs-winston
     * my log input will be ({message, metadata}) and ({message, error, metadata})
     */
    logger?: {
        log: (...args: any[]) => any
        error: (...args: any[]) => any
        debug: (...args: any[]) => any
    }

    /** opposum circuit breaker option https://nodeshift.dev/opossum/#circuitbreaker
     * global circuit breaker options setting for each endpoint.
     * circuit breaker is not enable by default set enable: true to enable it.
     */
    circuitBreaker?: Omit<CircuitBreakerType.Options, 'timeout'>
}

config force throw error, retry, and individual time out for each endpoint

try {
    const result = await client.functionName1(params, 
    { 
        forceThrowError:true, // default false. So it didn't throw error
        maxRetry: 2,
        timeoutMs: 10000 // 10s timeout ignore the global config
     })
} catch (error) {
    console.error(error)
    throw error
}
1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.1.0

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago