|
| 1 | +## Signed URL Plugin |
| 2 | + |
| 3 | +This plugin allows generating a signed url for a file link/id. Useful when |
| 4 | +using with AWS S3 sign urls for private objects. |
| 5 | + |
| 6 | +## Basic Usage |
| 7 | + |
| 8 | +- Register the plugin |
| 9 | + |
| 10 | +```js |
| 11 | +await server.register([ |
| 12 | + { |
| 13 | + plugin: signedUrl, |
| 14 | + options: { |
| 15 | + getSignedUrl: (key: string): string => 'my_custom_sign', // takes in function to sign the id |
| 16 | + }, |
| 17 | + }, |
| 18 | +]); |
| 19 | +``` |
| 20 | + |
| 21 | +- Dummy response |
| 22 | + |
| 23 | +```json |
| 24 | +{ |
| 25 | + "file": "random_id", |
| 26 | + "name": "this is a file" |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +- Create a lens using ramda for the above object |
| 31 | + |
| 32 | +```js |
| 33 | +const lens = R.lensProp<string, any>('file') // here file is the key from object |
| 34 | +``` |
| 35 | + |
| 36 | +- Use it in the route |
| 37 | + |
| 38 | +```js |
| 39 | +server.route({ |
| 40 | + method: 'GET', |
| 41 | + path: '/sample', |
| 42 | + options: { |
| 43 | + handler: handler.performAction, |
| 44 | + plugins: { |
| 45 | + // define the plugin |
| 46 | + signedUrl: { |
| 47 | + // add the array of lenses for multiple keys |
| 48 | + lenses: [lens], |
| 49 | + }, |
| 50 | + }, |
| 51 | + ... |
| 52 | + }, |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +- Final response |
| 57 | + |
| 58 | +```json |
| 59 | +{ |
| 60 | + "file": "random_id_SIGNATURE", // this value will be updated |
| 61 | + "name": "this is a file" |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### For nested objects |
| 66 | + |
| 67 | +```js |
| 68 | +{ |
| 69 | + name: "some name", |
| 70 | + more: "some more data", |
| 71 | + ..., |
| 72 | + documents: [ |
| 73 | + { |
| 74 | + asset: "thisisafile", // want to update this |
| 75 | + other: "other details", |
| 76 | + ... |
| 77 | + } |
| 78 | + ] |
| 79 | +} |
| 80 | + |
| 81 | +// notice we dont change the basic lens |
| 82 | +const lens = R.lensProp<string, any>('asset'); |
| 83 | + |
| 84 | +// but we add extra options, pathToSource |
| 85 | +// you can add multiple layer of paths |
| 86 | +const path = R.lensPath(['documents']); |
| 87 | + |
| 88 | +// in route it looks like |
| 89 | +server.route({ |
| 90 | + method: 'GET', |
| 91 | + path: '/sample', |
| 92 | + options: { |
| 93 | + handler: handler.performAction, |
| 94 | + plugins: { |
| 95 | + signedUrl: { |
| 96 | + lenses: [lens], |
| 97 | + pathToSource: path, // add the path to source |
| 98 | + }, |
| 99 | + }, |
| 100 | + ... |
| 101 | + }, |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +### Note |
| 106 | + |
| 107 | +- It will work with single objects and arrays. `pathToSource` is optional field, |
| 108 | + use when nested objects are to be updated. |
0 commit comments