|
| 1 | +# Utilty Library |
| 2 | + |
| 3 | +To assist with the usage of this Extension, a [utility library](https://www.npmjs.com/package/@invertase/storage-image-processing-api) exists |
| 4 | +to help provide a typed API interface for easily constructing operations to send to the API. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +```bash |
| 9 | +npm i --save @invertase/storage-image-processing-api |
| 10 | +``` |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +Once installed, import the `builder` function from the library: |
| 15 | + |
| 16 | +```ts |
| 17 | +import { builder } from '@invertase/storage-image-processing-api'; |
| 18 | +``` |
| 19 | + |
| 20 | +The `builder` function returns a new `StorageImageProcessingApi` instance which provides a API for constructing operations to send to the API. |
| 21 | +At a minimum, you must provide an `input` and `output` operation as required by the extension itself: |
| 22 | + |
| 23 | +```ts |
| 24 | +import { builder } from '@invertase/storage-image-processing-api'; |
| 25 | + |
| 26 | +const build = builder() |
| 27 | + .input({ |
| 28 | + url: 'https://example.com/image.jpg', |
| 29 | + }) |
| 30 | + .output({ |
| 31 | + format: 'png', |
| 32 | + }); |
| 33 | +``` |
| 34 | + |
| 35 | +To provide additional operations, you can chain them together. For example, |
| 36 | +to apply a blur, grayscale and flip the provided input image, and return a new PNG image: |
| 37 | + |
| 38 | +```ts |
| 39 | +const output = builder() |
| 40 | + .input({ |
| 41 | + url: 'https://example.com/image.jpg', |
| 42 | + }) |
| 43 | + .blur() |
| 44 | + .grayscale() |
| 45 | + .flip() |
| 46 | + .output({ |
| 47 | + format: 'png', |
| 48 | + }); |
| 49 | +``` |
| 50 | + |
| 51 | +Please refer to the [operations](/operations) documentatuion for a full list of available operations, their API and examples. |
| 52 | + |
| 53 | +Once you have constructed your operations, you can return the operations as JSON, a JSON string or encoded JSON string value: |
| 54 | + |
| 55 | +```ts |
| 56 | +const json = output.toJSON(); |
| 57 | +const jsonString = output.toJSONString(); |
| 58 | +const encodedJsonString = output.toEncodedJSONString(); |
| 59 | +``` |
| 60 | + |
| 61 | +The encoded JSON string value can be passed directly to the extension as the `operations` parameter: |
| 62 | + |
| 63 | +```ts |
| 64 | +const encodedJsonString = output.toEncodedJSONString(); |
| 65 | + |
| 66 | +const url = `https://{LOCATION}-{PROJECT_ID}.cloudfunctions.net/ext-storage-image-processing-api-handler/process?operations=${encodedJsonString}` |
| 67 | +``` |
0 commit comments