Fetch

Fetches and caches media resources and JSON data

interface CacheArgs  {
  audible?: boolean
  visible?: boolean
}
interface CacheInfo  {
  count: number
  createdAt: number
  updatedAt: number
}
interface CacheOptions 
extends CacheArgs {
  audible?: boolean
  quantize?: number
  size?: Size
  time?: Time
  visible?: boolean
}
interface DropResource 
extends StoredResource {
  bytes?: number
  createdAt?: number
  decoded?: UnknownRecord
  deletedAt?: number
  error?: NamedError
  extension: string
  id: string
  request?: EndpointRequest
  source: string
  storedAt?: number
  type: string
  updatedAt?: number
}
interface EndpointRequest  {
  endpoint: FetchUrl
  expires?: number
  init?: RequestInit
}
type FetchCacheFunction = (args: FetchCacheRequest) => Promise<DataOrError<boolean>>
interface FetchCacheRequest 
extends FetchCachedRequest {
  extension: string
  file: File
  id: string
}
type FetchCachedFunction = (args: FetchCachedRequest) => Promise<DataOrError<File>>
interface FetchCachedRequest  {
  extension: string
  id: string
}
type FetchFileChunk = string | ArrayBuffer
interface FetchFileMessage  {
  chunk: FetchFileChunk
  last: boolean
  name: string
}
type FetchJsonFunction = (args: EndpointRequest, opts?: JobOptions) => Promise<DataOrError<FetchJsonResponse>>
interface FetchJsonResponse  {
  jsonArray?: JsonArray
  jsonRecord?: JsonRecord
  modified?: number
}
interface FetchPlugin 
extends Plugable {
  cache: FetchCacheFunction
  cached: FetchCachedFunction
  install: function
  json: FetchJsonFunction
  resource: FetchResourceFunction
  text: FetchTextFunction
  uncache: FetchUncacheFunction
}
type FetchResourceFunction = (args: Resource, opts?: JobOptions) => Promise<DataOrError<true>>
type FetchUncacheFunction = (args: FetchCachedRequest) => Promise<DataOrError<boolean>>
type FetchUrl = HttpFetchUrl | HttpsFetchUrl | FileFetchUrl
type FetchUrlInit = ArrayOf2<string, RequestInit>
interface Fetched 
extends WithSource, WithRequest {
  request: EndpointRequest
  source: string
}
interface FetchedEstimate  {
  available: number
  total: number
  used: number
}
type FileFetchUrl = string
interface FilesCacheArgs 
extends CacheOptions {
  audible?: boolean
  commandFiles: Array<CommandFile>
  quantize?: number
  size?: Size
  time?: Time
  visible?: boolean
}
type HttpFetchUrl = string
type HttpsFetchUrl = string
type IdResourceFunction = () => string
interface InstanceCacheArgs 
extends CacheOptions {
  audible?: boolean
  clipTime: TimeRange
  quantize?: number
  size?: Size
  time?: Time
  visible?: boolean
}
type LocalFetchUrl = FileFetchUrl | AbsolutePath
interface MediaCacheArgs 
extends CacheOptions {
  assetTime?: Time
  audible?: boolean
  quantize?: number
  size?: Size
  time?: Time
  visible?: boolean
}
interface MediaResource 
extends WithId {
  id: string
  mediaId: string
}
type RemoteFetchUrl = HttpFetchUrl | HttpsFetchUrl
interface Resource 
extends Stored, ResourceProperties {
  asStored: StoredResource
  bytes?: number
  createdAt?: number
  decoded?: UnknownRecord
  deletedAt?: number
  download: (userIdAndId: UserIdAndId) => Promise<DataOrError<ApiDownloadResponse>>
  error?: NamedError
  extension: string
  fetch: (userIdAndId: UserIdAndId) => Promise<DataOrError<true>>
  id: string
  path?: AbsolutePath
  request?: EndpointRequest
  source: string
  storedAt?: number
  text?: string
  textPromise?: Promise<DataOrError<string>>
  type: string
  unload: () => void
  update: (storedResource: StoredResource | StorableResource) => void
  updatedAt?: number
  upload: (userIdAndId: UserIdAndId) => Promise<DataOrError<ApiUploadResponse>>
  urlPromise?: Promise<DataOrError<string>>
}
interface ResourceProperties  {
  bytes?: number
  decoded?: UnknownRecord
  deletedAt?: number
  error?: NamedError
  extension: string
  request?: EndpointRequest
  storedAt?: number
}
type ResourceType = CodeType | 'raw'
/**
 * Record with keys that are media IDs and
 * values that are arrays of stored
 * resources.
 */
type ResourcesByMediaId = Record<string, Array<StoredResource>>
interface StorableResource 
extends Storable, ResourceProperties {
  bytes?: number
  createdAt?: number
  decoded?: UnknownRecord
  deletedAt?: number
  error?: NamedError
  extension: string
  id?: string
  request?: EndpointRequest
  source: string
  storedAt?: number
  type: string
  updatedAt?: number
}
interface StorableResourceRecord 
extends Record<string, Array<StorableResource>> {}
interface StoreDeleteRequest  {
  accountId: string
  mediaId: string
  resources: Array<StoredResource>
}
interface StoreRequest  {
  accountId?: string
  extension?: string
  mediaId?: string
  mediaSource?: string
  mediaType?: string
  resourceId?: string
  resourceSource?: string
  resourceType?: string
  variables?: StringRecord
}
interface StoredResource 
extends Stored, ResourceProperties {
  bytes?: number
  createdAt?: number
  decoded?: UnknownRecord
  deletedAt?: number
  error?: NamedError
  extension: string
  id: string
  request?: EndpointRequest
  source: string
  storedAt?: number
  type: string
  updatedAt?: number
}
interface SystemAddFileRequest  {
  content: string | object
  dontReplace?: boolean
  path: AbsolutePath
}
interface SystemCopyRequest  {
  destination: AbsolutePath
  dontReplace?: boolean
  source: AbsolutePath
}
interface SystemMoveRequest  {
  from: AbsolutePath
  to: AbsolutePath
}
interface WithRequest  {
  request: EndpointRequest
}
/** @implements FetchPlugin */
class ClientFetchPlugin {
  cache: FetchCacheFunction
  cached: FetchCachedFunction
  install: function
  json: FetchJsonFunction
  resource: FetchResourceFunction
  text: FetchTextFunction
  uncache: FetchUncacheFunction
}
/** @implements FetchPlugin */
class FetchPluginClass
extends PluginClass {
  cache: FetchCacheFunction
  cached: FetchCachedFunction
  install: function
  json: FetchJsonFunction
  resource: FetchResourceFunction
  text: FetchTextFunction
  uncache: FetchUncacheFunction
}
/** @implements Resource */
class ResourceClass {
  asStored: StoredResource
  bytes?: number
  clientAudio: ClientAudio | undefined
  clientAudioPromise: Promise<DataOrError<ClientAudio>> | undefined
  clientFont: ClientFont | undefined
  clientFontPromise: Promise<DataOrError<ClientFont>> | undefined
  clientImage: ClientImage | undefined
  clientImagePromise: Promise<DataOrError<ClientImage>> | undefined
  clientVideo: ClientVideo | undefined
  clientVideoPromise: Promise<DataOrError<ClientVideo>> | undefined
  createdAt?: number
  decoded?: UnknownRecord
  deletedAt?: number
  download: (userIdAndId: UserIdAndId) => Promise<DataOrError<ApiDownloadResponse>>
  downloadPromise: (media: UserIdAndId) => Promise<DataOrError<ApiDownloadResponse>>
  downloadPromiseOrNot: Promise<DataOrError<ApiDownloadResponse>> | undefined
  error?: NamedError
  extension: string
  fetch: (userIdAndId: UserIdAndId) => Promise<DataOrError<true>>
  host: Plugins
  id: string
  objectUrl: string | undefined
  path?: AbsolutePath
  request?: EndpointRequest
  source: string
  storedAt?: number
  string: string | undefined
  text?: string
  textPromise?: Promise<DataOrError<string>>
  type: string
  unload: () => void
  update: (storedResource: StoredResource | StorableResource) => void
  updatedAt?: number
  upload: (userIdAndId: UserIdAndId) => Promise<DataOrError<ApiUploadResponse>>
  uploadPromise: (media: UserIdAndId) => Promise<DataOrError<ApiUploadResponse>>
  uploadPromiseOrNot: Promise<DataOrError<ApiUploadResponse>> | undefined
  urlPromise?: Promise<DataOrError<string>>
}
function apiRequestFromOptions(
  apiOptions: ApiOptions, 
  endpointId: ApiEndpointId, 
  body?: object
): Promise<DataOrError<EndpointRequest>>
/** Type assertion for RemoteFetchUrl */
function assertRemoteFetchUrl(
  value: unknown, 
  name?: string
): void
function cachePromises(
  promises: Array<Promise<DataOrError<number>>>
): Promise<DataOrError<number>>
function canvasContext(
  size: Size
): CanvasContextTuple
/** Type guard for ExpressAuthInfo */
function isAuthedRequest(
  req: unknown
): boolean
/** Type guard for EndpointRequest */
function isEndpointRequest(
  value: unknown
): boolean
/** Type guard for FetchUrl */
function isFetchUrl(
  value: unknown
): boolean
/** Type guard for FileFetchUrl */
function isFileFetchUrl(
  value: unknown
): boolean
/** Type guard for HttpFetchUrl */
function isHttpFetchUrl(
  value: unknown
): boolean
/** Type guard for HttpsFetchUrl */
function isHttpsFetchUrl(
  value: unknown
): boolean
/** Type guard for MediaResource */
function isIdentifiedResource(
  value: unknown
): boolean
/** Type guard for RemoteFetchUrl */
function isRemoteFetchUrl(
  url: unknown
): boolean
/** Type guard for Resource */
function isResource(
  value: unknown
): boolean
/** Type guard for ResourceType */
function isResourceType(
  value: Any
): boolean
/** Type guard for StorableResource */
function isStorableResource(
  value: unknown
): boolean
/** Type guard for StoredResource */
function isStoredResource(
  value: unknown
): boolean
function jsonParse(
  json: string
): void
function jsonStringify(
  value: unknown, 
  space?: string | number
): string
function requestFromConfig(
  config: string | EndpointRequest | OrArray<StoredMedia> | undefined, 
  host: Plugins
): EndpointRequest | OrArray<StoredMedia> | undefined
function resourceSizeInvalid(
  bytes: number, 
  type: FileType, 
  uploadLimits?: Partial<Record<FileType, number>>
): number
function urlName(
  url: string
): string