This project implements TypeScript definitions and API helpers for the Fair&Smart Right Consents API. It's meant for use in Right Consents Front-end, but should be general enough to use in any project. The code is maintained and based on the Right Consents Back-end API resources and models, to be as close as possible to the true representation of the resources.
Before mergin into master, please ensure you:
- updated
versioninpackage-lib.json - run
npm run pack - embed in your MR what has been changed under lib/, _bundles/, docs/
v3.x is targetted for node 18. v2.x is targetted for node 14 and is deprecated.
- v3.x+: known to work using node v18.20.8 (npm v10.8.2). Use NVM to force version if needed.
- v2.x-: known to work using node v14.21.3 (npm v6.14.18). Use NVM to force version if needed.
npm i @fairandsmart/consents-ce
All the interface type definitions and enums are for type info only - everything will compile out. Only the API helpers produce real JavaScript output. You can import types from each service defined on the API:
import { ModelVersionDtoLight, ModelsResource } from '@fairandsmart/consents-ce/models';In addition to the types, there are also simple helper functions for each API endpoint. They define the inputs and outputs to that endpoint, and will call a user-provided function with HTTP request info that you can then use to make an HTTP request. This pattern was used so the API helpers could provide full type information. These helpers are not a full API client - they assist in building one. An example:
import { RcHttpClientConfig } from '@fairandsmart/consents-ce';
function http(config: RcHttpClientConfig): Observable<ModelEntryDto> {
// fill in the API key, handle OAuth, etc., then make an HTTP request using the config.
return fromFetch(config.url, ...);
}
// Use either as an observable
ModelsResource.getEntry(entryId).subscribe((modelEntry: ModelEntryDto) => {
...
});
// ... or a promise using the .toPromise method
const modelEntry: ModelEntryDto = await ModelsResource.getEntry(entryId).toPromise();Note that the API Helpers all return Observables and not promises. Feel free to adapt the behavior as you see fit.
To setup the helpers properly, see the Initialization section below.
Note: this is not required if you only want to use the library for type descriptions
If you wish to use the API helpers, you need to initialize the library in your project:
import { RightConsents } from '@fairandsmart/consents-ce';
RightConsents.init({
apiRoot: 'http://localhost:4287',
httpClient: myCustomHttpClient // see API Helpers for more details
})You need to provide the endpoint at which the API is available, and a custom HttpClient to handle all the requests.
It is possible to import all services from @fairandsmart/consent-manager directly, but it's better to use the direct import:
// good
import { getEntry } from '@fairandsmart/consents-ce/models';
getEntry(...);
// works, but not as good. Use for disambiguity if needed
import { ModelsResources } from '@fairandsmart/consents-ce';
ModelsResources.getEntry(...);Some imports also contains helpers for parsing and interpreting the resources:
import { map } from 'rxjs';
import { ModelEntryDto, ModelEntryHelper, getEntry } from '@fairandsmart/consents-ce/models';
getEntry(modelId).pipe(
map((entry: ModelEntryDto) => ModelEntryHelper.getActiveVersion(entry))
).subscribe((activeVersion) => {
// ModelEntryHelper is an object with static methods to help you parse the entry.
// In this example, you can find the active version of a model
...
});# build
npm run build
# pack for local import
npm run pack
# install local version in a different project
npm i {lib-repository}/lib/fairandsmart-consents-ce-{VERSION}.tgz
- Create a branch for your work (PRD-XXX)
- Upgrade the version in the package-lib.json
- Add your work
- Run
.build.shand push - Create a new merge request