All files / src convenience.ts

100% Statements 14/14
100% Branches 6/6
100% Functions 8/8
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                                                                2x                 3x 3x                                 2x 2x                 1x 1x                 2x               2x 2x 2x 2x           10x 10x        
import { BlockBlobClient } from './block-blob-client'
import type { AzureClient } from './client'
import type { ListOptions } from './internal/paging'
import type { StorageClientOptions } from './internal/storage-request'
import type { TableEntity, TableEntityResult } from './internal/table-entity'
import { TableClient } from './table-client'
import type {
  TableEntityQueryOptions,
  TableEntityWriteResponse,
  TableUpdateMode,
} from './table-client'
 
export interface BlobUploadTextOptions {
  /** Defaults to `text/plain; charset=utf-8`. */
  contentType?: string
}
 
export interface BlobDownloadTextResponse {
  text: string
}
 
export interface BlobDownloadJsonResponse<T> {
  value: T
}
 
export interface TableGetEntityResponse {
  entity?: TableEntityResult
}
 
export interface TableEntityPage {
  entities: TableEntityResult[]
  continuationToken?: string
}
 
export async function uploadText(
  client: AzureClient,
  blobUrl: string,
  text: string,
  options: BlobUploadTextOptions = {},
): Promise<Response> {
  return await new BlockBlobClient(blobUrl, undefined, through(client)).upload(text, {
    contentType: options.contentType ?? 'text/plain; charset=utf-8',
  })
}
 
export async function downloadText(
  client: AzureClient,
  blobUrl: string,
): Promise<BlobDownloadTextResponse> {
  const response = await new BlockBlobClient(blobUrl, undefined, through(client)).download()
  return { text: await response.text() }
}
 
export async function downloadJson(
  client: AzureClient,
  blobUrl: string,
): Promise<BlobDownloadJsonResponse<unknown>>
export async function downloadJson<T>(
  client: AzureClient,
  blobUrl: string,
  parse: (value: unknown) => T,
): Promise<BlobDownloadJsonResponse<T>>
export async function downloadJson<T>(
  client: AzureClient,
  blobUrl: string,
  parse?: (value: unknown) => T,
): Promise<BlobDownloadJsonResponse<unknown>> {
  const value: unknown = JSON.parse((await downloadText(client, blobUrl)).text)
  return { value: parse === undefined ? value : parse(value) }
}
 
export async function getEntity(
  client: AzureClient,
  tableUrl: string,
  partitionKey: string,
  rowKey: string,
): Promise<TableGetEntityResponse> {
  const table = new TableClient(tableUrl, undefined, through(client))
  return { entity: await table.getEntity(partitionKey, rowKey) }
}
 
export async function upsertEntity(
  client: AzureClient,
  tableUrl: string,
  entity: TableEntity,
  mode?: TableUpdateMode,
): Promise<TableEntityWriteResponse> {
  return await new TableClient(tableUrl, undefined, through(client)).upsertEntity(entity, mode)
}
 
export async function* listEntitiesPage(
  client: AzureClient,
  tableUrl: string,
  { filter, select, ...pageOptions }: ListOptions & TableEntityQueryOptions = {},
): AsyncGenerator<TableEntityPage> {
  const table = new TableClient(tableUrl, undefined, through(client))
  const pages = table.listEntities({ filter, select }).byPage(pageOptions)
  for await (const { value, continuationToken } of pages) {
    yield { entities: value, continuationToken }
  }
}
 
/** Storage clients without a credential send through the AzureClient, which authorizes and retries. */
function through(client: AzureClient): StorageClientOptions {
  return {
    fetch: async (input, init) => await client.fetch(input, init),
    retryOptions: { maxRetries: 0 },
  }
}