diff --git a/lib/facts/blob-service.js b/lib/facts/blob-service.js new file mode 100644 --- /dev/null +++ b/lib/facts/blob-service.js @@ -0,0 +1,32 @@ +// @flow + +export type BlobServiceHTTPEndpoint = { + +path: string, + +method: 'GET' | 'POST' | 'PUT' | 'DELETE', +}; + +const httpEndpoints = Object.freeze({ + GET_BLOB: { + path: '/blob/:holder', + method: 'GET', + }, + ASSIGN_HOLDER: { + path: '/blob', + method: 'POST', + }, + UPLOAD_BLOB: { + path: '/blob', + method: 'PUT', + }, + DELETE_BLOB: { + path: '/blob/:holder', + method: 'DELETE', + }, +}); + +const config = { + url: 'https://blob.commtechnologies.org', + httpEndpoints, +}; + +export default config; diff --git a/lib/utils/blob-service.js b/lib/utils/blob-service.js new file mode 100644 --- /dev/null +++ b/lib/utils/blob-service.js @@ -0,0 +1,54 @@ +// @flow + +import invariant from 'invariant'; + +import type { BlobServiceHTTPEndpoint } from '../facts/blob-service.js'; +import blobServiceConfig from '../facts/blob-service.js'; + +const BLOB_SERVICE_URI_PREFIX = 'comm-blob-service://'; + +function makeBlobServiceURI(holder: string): string { + return `${BLOB_SERVICE_URI_PREFIX}${holder}`; +} + +function isBlobServiceURI(uri: string): boolean { + return uri.startsWith(BLOB_SERVICE_URI_PREFIX); +} + +function holderFromBlobServiceURI(uri: string): string { + invariant(isBlobServiceURI(uri), 'Not a blob service URI'); + return uri.slice(BLOB_SERVICE_URI_PREFIX.length); +} + +function holderFromURI(uri: string): ?string { + if (!isBlobServiceURI(uri)) { + return null; + } + return holderFromBlobServiceURI(uri); +} + +function makeBlobServiceEndpointURL( + endpoint: BlobServiceHTTPEndpoint, + params: { +[name: string]: string } = {}, +): string { + let path = endpoint.path; + for (const name in params) { + path = path.replace(`:${name}`, params[name]); + } + return `${blobServiceConfig.url}${path}`; +} + +function getBlobFetchableURL(holder: string): string { + return makeBlobServiceEndpointURL(blobServiceConfig.httpEndpoints.GET_BLOB, { + holder, + }); +} + +export { + makeBlobServiceURI, + isBlobServiceURI, + holderFromURI, + holderFromBlobServiceURI, + getBlobFetchableURL, + makeBlobServiceEndpointURL, +};