@flexbase/http-client-middleware
Version:
Http client middleware
72 lines (53 loc) • 2.32 kB
Markdown
[](https://sonarcloud.io/summary/new_code?id=flexbase-eng_http-client-middleware)
[](https://sonarcloud.io/summary/new_code?id=flexbase-eng_http-client-middleware)
# http-client-middleware
This is a middleware package to wrap http authentication.
## Usage
Using [wretch](https://github.com/elbywan/wretch)
### Client Credentials
```typescript
const clientCredentials = {
tokenUrl: "https://some.url/auth/token",
refreshTokenUrl: "https://some.url/auth/refresh",
clientId: "myclient id",
clientSecret: "super secret"
};
class SimpleAuthenticationTokenStore implements AuthenticationTokenStore {
private _authenticationToken: AuthenticationToken | null = null;
retrieveToken(): IAuthenticationToken | null {
return this._authenticationToken;
}
storeToken(token: IAuthenticationToken | null): void {
this._authenticationToken = token;
}
}
const authMiddleware = authenticationTokenMiddleware({
credentialProvider: () => clientCredentials,
tokenAccessor: new ClientCredentialsAuthenticationTokenAccessor(),
tokenStore: new SimpleAuthenticationTokenStore()
});
wretch(...).middlewares([authMiddleware]);
```
### Password Credentials
```typescript
// for password creds we only need to supply the urls as user/pass will come from a login form
const passwordCredentials = {
tokenUrl: "https://some.url/auth/token",
refreshTokenUrl: "https://some.url/auth/refresh",
};
class SimpleAuthenticationTokenStore implements AuthenticationTokenStore {
private _authenticationToken: AuthenticationToken | null = null;
retrieveToken(): IAuthenticationToken | null {
return this._authenticationToken;
}
storeToken(token: IAuthenticationToken | null): void {
this._authenticationToken = token;
}
}
const authMiddleware = authenticationTokenMiddleware({
credentialProvider: () => passwordCredentials,
tokenAccessor: new PasswordAuthenticationTokenAccessor(),
tokenStore: new SimpleAuthenticationTokenStore()
});
wretch(...).middlewares([authMiddleware]);
```