|
| 1 | +# Client-Side OFREP Provider |
| 2 | + |
| 3 | +This provider is designed to use the [OpenFeature Remote Evaluation Protocol (OFREP)](https://openfeature.dev/specification/appendix-c). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +### npm |
| 8 | + |
| 9 | +```sh |
| 10 | +npm install @openfeature/ofrep-web |
| 11 | +``` |
| 12 | + |
| 13 | +### yarn |
| 14 | + |
| 15 | +```sh |
| 16 | +yarn add @openfeature/ofrep-web @openfeature/ofrep-core @openfeature/web-sdk |
| 17 | +``` |
| 18 | + |
| 19 | +> [!NOTE] |
| 20 | +> yarn requires manual installation of peer dependencies |
| 21 | +
|
| 22 | +## Configurations and Usage |
| 23 | + |
| 24 | +The provider needs the base url of the OFREP server for instantiation. |
| 25 | + |
| 26 | +```ts |
| 27 | +import { OFREPWebProvider } from '@openfeature/ofrep-web'; |
| 28 | + |
| 29 | +OpenFeature.setProvider(new OFREPWebProvider({ baseUrl: 'https://localhost:8080', pollingInterval: 60000 })); |
| 30 | +``` |
| 31 | + |
| 32 | +### HTTP headers |
| 33 | + |
| 34 | +The provider can use headers from either a static header map or a custom header factory. |
| 35 | + |
| 36 | +#### Static Headers |
| 37 | + |
| 38 | +Headers can be given as a list of tuples or as a map of headers. |
| 39 | + |
| 40 | +```ts |
| 41 | +import { OFREPWebProvider } from '@openfeature/ofrep-web'; |
| 42 | + |
| 43 | +OpenFeature.setProvider( |
| 44 | + new OFREPWebProvider({ |
| 45 | + baseUrl: 'https://localhost:8080', |
| 46 | + headers: [ |
| 47 | + ['Authorization', `my-api-key`], |
| 48 | + ['X-My-Header', `CustomHeaderValue`], |
| 49 | + ], |
| 50 | + }), |
| 51 | +); |
| 52 | +``` |
| 53 | + |
| 54 | +```ts |
| 55 | +import { OFREPWebProvider } from '@openfeature/ofrep-web'; |
| 56 | + |
| 57 | +OpenFeature.setProvider( |
| 58 | + new OFREPWebProvider({ |
| 59 | + baseUrl: 'https://localhost:8080', |
| 60 | + headers: { Authorization: `my-api-key`, 'X-My-Header': `CustomHeaderValue` }, |
| 61 | + }), |
| 62 | +); |
| 63 | +``` |
| 64 | + |
| 65 | +#### Header Factory |
| 66 | + |
| 67 | +The header factory is evaluated before every flag evaluation which makes it possible to use dynamic values for the headers. |
| 68 | + |
| 69 | +The following shows an example of loading a token and using it as bearer token. |
| 70 | + |
| 71 | +```ts |
| 72 | +import { OFREPWebProvider } from '@openfeature/ofrep-web'; |
| 73 | + |
| 74 | +OpenFeature.setProvider( |
| 75 | + new OFREPWebProvider({ |
| 76 | + baseUrl: 'https://localhost:8080', |
| 77 | + headersFactory: () => { |
| 78 | + const token: string = loadDynamicToken(); |
| 79 | + return [['Authorization', `Bearer ${token}`]]; |
| 80 | + }, |
| 81 | + }), |
| 82 | +); |
| 83 | +``` |
| 84 | + |
| 85 | +### Fetch implementation |
| 86 | + |
| 87 | +If needed, a custom fetch implementation can be injected, if e.g. the platform does not have fetch built in. |
| 88 | + |
| 89 | +```ts |
| 90 | +import { OFREPWebProvider } from '@openfeature/ofrep-web'; |
| 91 | +import { fetchPolyfill } from 'some-fetch-polyfill'; |
| 92 | + |
| 93 | +OpenFeature.setProvider( |
| 94 | + new OFREPWebProvider({ |
| 95 | + baseUrl: 'https://localhost:8080', |
| 96 | + fetchImplementation: fetchPolyfill |
| 97 | + }), |
| 98 | +); |
| 99 | +``` |
| 100 | + |
| 101 | +## Building |
| 102 | + |
| 103 | +Run `nx package providers-ofrep-web` to build the library. |
| 104 | + |
| 105 | +## Running unit tests |
| 106 | + |
| 107 | +Run `nx test providers-ofrep-web` to execute the unit tests via [Jest](https://jestjs.io). |
0 commit comments