/** * Smartface Service-Call helper module * @module service-call * @type {object} * @deprecated USE AXIOS OR XMLHTTPREQUEST INSTEAD! * @copyright Smartface 2021 */ import Http from "@smartface/native/net/http"; interface IRequestOptions { /** * HTTP method of this request */ method: string; /** * Request payload body. This object will be automatically stringified */ body?: { [key: string]: any; } | string; /** * Query string string object. Combines with the url */ q?: string; /** * Alias for options.q */ query?: string; /** * Request specific headers. In conflict with configuration, those values are used */ headers?: { [key: string]: any; } | string; /** * Request specific log option */ logEnabled?: boolean; /** * Basic authentication user. Must be used with options.password */ user?: string; /** * Basic authentication password. Must be used with options.user */ password?: string; /** * Resolved promise contains full response including `headers`, `body` and `status` */ fullResponse?: boolean; url?: string; } interface IServiceCallParameters { baseUrl: string; timeout?: number; logEnabled?: boolean; headers?: { [key: string]: string; }; sslPinning?: Http["ios"]["sslPinning"]; } /** * Helper class for calling JSON based restful services. * @public */ export default class ServiceCall { protected _http: Http; /** * Base URL for this service-call library uses. This can be get and set * @property {string} baseUrl */ private _baseUrl; /** * Log enabled for service-call. This can be get and set * @property {boolean} logEnabled */ private _logEnabled; /** * Creates a ServiceCall helper class with common configuration to be used across multiple service calls. * @param {object} options - Cofiguration of service call helper object (required) * @param {string} options.baseUrl - Base URL of all future service calls (required) * @param {number} [options.timeout = 60000] - Timeout value for service calls. If not provided it uses the default timeout value from @smartface/native http * @param {boolean} [options.logEnabled = false] - Logs the service requests & responses to console * @param {object} [options.headers] - Sets the default headers for this configuration * @example *``` * // services/serviceConfig.ts * import ServiceCall from '@smartface/extension-utils/lib/service-call'; * export const sc = new ServiceCall({ * baseUrl: "http://api.myBaseUrl.com", * logEnabled: true, * headers: { * apiVersion: "1.0" * } * }); * * // services/user.ts * import { sc } from 'services/serviceConfig"'; * * function login(userName, password) { * return new Promise((resolve, reject) => { * sc.request(`/auth/login?emine=3`, { * method: "POST", * body: { * userName, * password * } * }).then(response => { * sc.setHeader("Authorization", "Bearer " + response.token); * resolve(response); * }).catch(err => { * reject(err); * }); * }); * } * * * // pages/pgLogin.ts * import userService from 'services/user'; * * this.btnLogin.onPress = () => { * userService.login(this.tbUserName.text, this.tbPassword.text).then(()=> { * this.router.push("dashboard"); * }).catch(()=> { * alert("Cannot login"); * }); * }; * ``` */ constructor(options: IServiceCallParameters); /** * Sets headers for this configuration. Either sets one by each call or sets them in bulk * @method * @param {string} key - Header name to set * @param {string} value - Value to set of the key. If value is not a string, key is removed from header * @example * ``` * //After login * sc.setHeader("Authorization", "Basic 12345"); * ``` * @example * ``` * //After logout * sc.setHeader("Authorization"); * ``` * @example * ``` * // set multiple headers at once * sc.setHeader({ * environment: "test", * apiVersion: "1.2" //replaces the existing * }); * ``` */ setHeader(key: string | Record, value?: string): void; /** * Gets a copy of headers used * @method * @returns {object} headers */ getHeaders(): Record; /** * Base URL for this service-call library uses. This can be get and set * @property {string} baseUrl */ get baseUrl(): string; set baseUrl(value: string); /** * Log enabled for service-call. This can be get and set * @property {boolean} logEnabled */ get logEnabled(): boolean; set logEnabled(value: boolean); /** * creates a request options object for http request * @method * @example * ``` * const reqOps = sc.createRequestOptions(`/auth/login`, { * method: "POST", * body: { * userName, * password * }, * headers: { * "Content-Type": "application/json" * } * }); * sc.request(reqOps).then((result) => { * //logic * }).catch((err) => { * //logic * }); * ``` */ createRequestOptions(endpointPath: string, options: IRequestOptions): IRequestOptions; /** * Performs a service call and returns a promise to handle * @method * @example * ``` * const reqOps = sc.createRequestOptions(`/auth/login`, { * method: "POST", * body: { * userName, * password * }, * headers: { * "Content-Type": "application/json" * } * }); * sc.request(reqOps).then((result) => { * //logic * }).catch((err) => { * //logic * }); * ``` */ request(endpointPath: string, options: IRequestOptions): Promise; /** * Default values of headers * @static * @readonly * @property {object} header object */ static readonly BASE_HEADERS: Readonly<{ "Content-Type": string; Accept: string; "Accept-Language": any; "Cache-Control": string; }>; static bodyParser(requestUrl: string, response: any): void; static convertObjectToFormData(body: { [key: string]: any; }): string; static getContentType(headers?: Record): any; } export {};