UNPKG

@salaxy/node

Version:

Salaxy general plain JavaScript / TypeScript libraries with Node.js -ajax component (Palkkaus.fi).

208 lines (203 loc) 8.66 kB
import { Ajax, OAuthMessage, Test, Accounts, Workers, Calculator, Calculations, Taxcards, Reports, Files, CalendarEvents } from '@salaxy/core'; /** * Provides wrapper methods for communicating with the Palkkaus.fi API * The request-promise access to the server methods: GET, POST and DELETE * with different return types and authentication / error events. * This is implementation is the default for server-side running of the Salaxy library code. */ declare class AjaxNode implements Ajax { /** * By default credentials are not used in http-calls. * Enable credentials with this flag (force disabled when the token is set). */ useCredentials: boolean; /** * The server address - root of the server. This is settable field. * Will probably be changed to a configuration object in the final version. */ serverAddress: string; private token; /** * Creates a new instance of AjaxNode */ constructor(); /** Gets the Server address that is used as bases to the HTML queries. E.g. 'https://test-api.salaxy.com' */ getServerAddress(): string; /** Gets the API address with version information. E.g. 'https://test-api.salaxy.com/v02/api' */ getApiAddress(): string; /** * Gets a JSON-message from server using the API * * @param method - The API method is the url path after the api version segments (e.g. '/v02/api') * and starts with a forward slash, e.g. '/calculator/new', or a full URL address. * * @returns A Promise with result. Standard Promise rejection to be used for error handling. */ getJSON(method: string): Promise<any>; /** * Gets a HTML-message from server using the API * * @param method - The API method is the url path after the api version segments (e.g. '/v02/api') * and starts with a forward slash, e.g. '/calculator/new', or a full URL address. * * @returns A Promise with result html. Standard Promise rejection to be used for error handling. */ getHTML(method: string): Promise<string>; /** * POSTS data to server and receives back a JSON-message. * * @param method - The API method is the url path after the api version segments (e.g. '/v02/api') * and starts with a forward slash, e.g. '/calculator/new', or a full URL address. * @param data - The data that is posted to the server. * * @returns A Promise with result. Standard Promise rejection to be used for error handling. */ postJSON(method: string, data: any): Promise<any>; /** * POSTS data to server and receives back HTML. * * @param method - The API method is the url path after the api version segments (e.g. '/v02/api') * and starts with a forward slash, e.g. '/calculator/new', or a full URL address. * @param data - The data that is posted to the server. * * @returns A Promise with result. Standard Promise rejection to be used for error handling. */ postHTML(method: string, data: any): Promise<string>; /** * Sends a DELETE-message to server using the API * * @param method - The API method is the url path after the api version segments (e.g. '/v02/api') * and starts with a forward slash, e.g. '/calculator/new', or a full URL address. * * @returns A Promise with result. Standard Promise rejection to be used for error handling. */ remove(method: string): Promise<any>; /** * Returns current JWT token. */ getCurrentToken(): string; /** * Sets token. * @param token - JWT token. */ setCurrentToken(token: string): void; /** * Fixes stack trace in Axios * Fixes this issue: https://github.com/axios/axios/issues/2387 * Taken from: https://github.com/rafaelalmeidatk/TIL/issues/4 */ axiosCatch: (stackTrace: any) => (error: any) => never; /** * Gets a new stacktrace at the current location. * Fixes this issue: https://github.com/axios/axios/issues/2387 * Taken from: https://github.com/rafaelalmeidatk/TIL/issues/4 */ getStackTrace(): string; /** If missing, append the API server address to the given url method string */ private getUrl; } /** * Configuration for the test helper: * Which server to contact, the credentials etc. */ interface TestHelperConfig { /** * Server to connect to. * Default / recommended is "auto", which currently points to test, * but may later be directed to a separte unit test environment. */ server?: "auto" | "test" | "prod" | "localhost"; /** * Possibility to set the server URL and port explicitly. * This setting overrides the server property, but is typically null: Used only in special situations. */ serverUrl?: string; /** Username that is used to authenticate the connection. */ username?: string; /** Password that is used to authenticate the connection. */ password?: string; /** * Test timeout in milliseconds. Used for calling Mocha.ISuiteCallbackContext.timeout() if provided. * The default is currently 30000, but it may be changed without it being a breaking change, so configure yout own, if you need a specific value. */ timeout?: number; } /** * Helpers for creating and runing tests in Node (server) environment. * Unit tests, System tests and demonstrations written in test format. * Tested and designed for Mocha / Chai, but should apply to other frameworks as well. */ declare class TestHelper { /** * Ajax instance that is used in this test session. */ ajax: AjaxNode; /** Configuration for the ajax connection */ config: TestHelperConfig; /** * Creates a new TestHelper with server configured, but not authenticated (call authenticate() to do that). * Anonymous mode is enough for many unit testing scenarios but not for e.g. CRUD operations. * We do not create an authenticated connection in constructor as a best practice. * * If suite callback context is specified, the timeout is set to config.timeout or 30000 as default. * The default number may be changed later without it being a breaking change, so set in config if you need it to be something specific. * * @param config Configuration for the test helper. * @param suiteCallBackContext Callback context is the **this** of 'describe(\"\", function ()' in Mocha. * See Mocha.ISuiteCallbackContext for details. * NOTE that you cannot use 'describe(\"\", () =>', because that will pass the wrong context (this). * @example * ```js * import { config } from "../config"; * * describe("Basic tests demonstrate the use of tester methods.", function() { * const sxyHelper = new TestHelper(config, this); * ``` */ constructor(config?: TestHelperConfig, suiteCallBackContext?: any); /** * Initializes the authenticated connection to the test server according to config. * Before calling this method, the ajax connection is anonymous. * Note that in Mocha / Chai, you can attach the method directly to before(), see examples below. * * @example * ```js * // Mocha / Chai testing * describe("Calculations basic CRUD operations.", function() { * const testHelper = new TestHelper(config, this); * before("Authenticate", testHelper.authenticate); *``` * * @example * ```js * const testHelper = new TestHelper(config); * testHelper.authenticate().then(() => { * // Some other initialization code here. * }); * ``` */ authenticate: () => Promise<OAuthMessage>; /** Shortcut to get the Salaxy OpenApi wrappers with the initialized ajax. */ get api(): { /** Test API */ test: Test; /** Accounts API */ accounts: Accounts; /** Workers CRUD API */ workers: Workers; /** Calculator */ calculator: Calculator; /** Calculations CRUD API */ calculations: Calculations; /** Taxcards API */ taxcards: Taxcards; /** Reports API */ reports: Reports; /** Files API */ files: Files; /** Calendar events API */ calendarEvents: CalendarEvents; }; private getServer; } export { AjaxNode, TestHelper, TestHelperConfig };