UNPKG

hfs-utilities

Version:

Health Fund Solution's internal utilities library for Typescript projects

158 lines (130 loc) 6.89 kB
[![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release) [![License: ISC](https://img.shields.io/badge/License-ISC-blue.svg)](https://opensource.org/licenses/ISC) ![Dependabot](https://img.shields.io/badge/dependabot-025E8C?style=for-the-badge&logo=dependabot&logoColor=white) ![GitHub Actions](https://img.shields.io/badge/github%20actions-%232671E5.svg?style=for-the-badge&logo=githubactions&logoColor=white) ![Gulp](https://img.shields.io/badge/GULP-%23CF4647.svg?style=for-the-badge&logo=gulp&logoColor=white) ![Azure](https://img.shields.io/badge/azure-%230072C6.svg?style=for-the-badge&logo=microsoftazure&logoColor=white) ![Visual Studio Code](https://img.shields.io/badge/Visual%20Studio%20Code-0078d7.svg?style=for-the-badge&logo=visual-studio-code&logoColor=white) ![TypeScript](https://img.shields.io/badge/typescript-%23007ACC.svg?style=for-the-badge&logo=typescript&logoColor=white) ![ESLint](https://img.shields.io/badge/ESLint-4B3263?style=for-the-badge&logo=eslint&logoColor=white) ![Mocha](https://img.shields.io/badge/-mocha-%238D6748?style=for-the-badge&logo=mocha&logoColor=white) ![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white) [![TS-Standard - Typescript Standard Style Guide](https://badgen.net/badge/code%20style/ts-standard/blue?icon=typescript)](https://github.com/standard/ts-standard) # hfs-utilities Utilities library for Health Fund Solutions Typescript projects. ## BlockBlobService examples: ```ts // Import the library import { BlockBlobService } from 'hfs-utilities' // Create a new instance of the service. Optionally provide connection string and container name. const blockBlobService: BlockBlobService = new BlockBlobService('INSERT PRIMARY CONNECTION STRING HERE') // Reads the blob contents, will attempt to parse to JSON if possible const blobContents: any = await blockBlobService.read('blobFile.txt', 'containerName') // Write the blob contents await blockBlobService.write('blobFile.txt', 'containerName', 'Hello World!') // Delete the blob await blockBlobService.delete('blobFile.txt', 'containerName') // Delete the container await blockBlobService.deleteContainer('containerName') // Create the container await blockBlobService.createContainer('containerName') // Lists the blobs in the container const blobs: string[] = await blockBlobService.listBlobs('containerName') // Checks if the container exists const containerExists: boolean = await blockBlobService.containerExists('containerName') // Checks if the blob exists const blobExists = await blockBlobService.blobExists('blobFile.txt', 'containerName') // Lists the containers in a storage account const containers: string[] = await blockBlobService.listContainers() ``` > **_NOTE:_** If working within one container, it is more convenient to pass the container name into the constructor right away, and then you do not need to pass it to any of the other methods. ## AzureDataTables examples: ```ts // Import the library import { AzureDataTables } from 'hfs-utilities' const azureDataTables: AzureDataTables = new AzureDataTables('INSERT PRIMARY CONNECTION STRING HERE', 'tableName') // toTableRow converts any object to a table row by stringifying it's values const tableRow: TableRow = azureDataTables.toTableRow({ primaryKey: 'primaryKey', rowKey: 'rowKey', data: { column1: 'value1', column2: ['value2'], } id: 1 }) // isTableRecord checks if the object is a valid table record, i.e. has a partitionKey & rowKey that are strings with // length > 0 and all properties are valid data types // will return true const isTableRecord: boolean = azureDataTables.isTableRecord(tableRow) // will return false const isNotTableRecord: boolean = azureDataTables.isTableRecord({}) // returns an array of one or more responses from the azure data tables service const response: TableTransactionResponse[] = await azureDataTables.upsert(tableRow) console.log(response) // would expect // [{ // status: 202, // subResponses: [ [Object], [Object] ], // getResponseForEntity: [Function: getResponseForEntity] // }] // delete a table row const response: TableTransactionResponse[] = await azureDataTables.delete(tableRow) // create a table row const response: TableTransactionResponse[] = await azureDataTables.create(tableRow) const results = await azureDataTables.query('SELECT * FROM tableName') console.log(results) // would expect // [{ // etag: `W/"datetime'2022-06-17T13%3A18%3A17.1793353Z'"`, // partitionKey: 'P2', // rowKey: 'R178', // timestamp: '2022-06-17T13:18:17.1793353Z' // }, // { // etag: `W/"datetime'2022-06-17T13%3A18%3A17.2303059Z'"`, // partitionKey: 'P2', // rowKey: 'R179', // timestamp: '2022-06-17T13:18:17.2303059Z' // }] ``` ## Utilities examples: ```ts // Import the library import { isEmpty, isNullOrUndefined, isNotNullAndNotUndefined, asArray, is, isEmptyString, isNotEmptyString, isString, } from 'hfs-utilities' // isEmpty checks if the value is empty const isEmpty: boolean = isEmpty('') // true const isNotEmpty: boolean = isNotEmpty('test') // false // isNullOrUndefined checks if the value is null or undefined const isNullOrUndefined: boolean = isNullOrUndefined(null) // true const isNotNullOrUndefined: boolean = isNotNullOrUndefined('test') // false // isNotNullAndNotUndefined checks if the value is not null or undefined const isNotNullAndNotUndefined: boolean = isNotNullAndNotUndefined('test') // true const isNullAndNotUndefined: boolean = isNullAndNotUndefined(null) // false // asArray converts a value to an array if it is not already an array const asArray: string[] = asArray('test') // returns ['test'] const asArray: string[] = asArray(['test']) // returns ['test'] // is checks if the value is of the specified type const isString: boolean = is('string', 'string') // true const isNotString: boolean = is('string', 2) // false // isEmptyString checks if the value when trimmed is an empty string const isEmptyString: boolean = isEmptyString(' ') // true const isNotEmptyString: boolean = isEmptyString('test') // false // isNotEmptyString checks if the value when trimmed is not an empty string const isEmptyString: boolean = isNotEmptyString(' ') // false const isNotEmptyString: boolean = isNotEmptyString('test') // true // isString checks if the value is a string const isString: boolean = isString('string') // true const isNotString: boolean = isString(2) // false ``` ## interfaces ```ts import { AzureFunctionSchema } from 'hfs-utilities' ```