@biblioteksentralen/media-store-api-client
v0.2.3
Published
Media Store API client
Downloads
26
Keywords
Readme
@biblioteksentralen/media-store-api-client
Installation
npm install @biblioteksentralen/media-store-api-client
Note that the package is a pure ESM package and doesn't provide a CommonJS export.
Machine-to-machine usage (backend/NodeJS)
With machine-to-machine (M2M) applications, such as scheduled jobs, where there is no user involved, the application can be authenticated and authorized using the Oauth Client Credentials Flow.
A helper method based on openid-client
is available from @biblioteksentralen/media-store-api-client/node
.
This cannot be imported in browser based environments.
import { MediaStoreApiClient } from "@biblioteksentralen/media-store-api-client";
import { clientCredentialsHelper } from "@biblioteksentralen/media-store-api-client/node";
const api = new MediaStoreApiClient({
userAgent: "my-little-script",
accessToken: clientCredentialsHelper({
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
}),
});
const assets = await api.assets.listAssets();
Note: Importing from @biblioteksentralen/media-store-api-client/node
requires "moduleResolution": "node16"
(or "nodenext"
) or newer in tsconfig.json
.
Browser usage
In browsers, an access token for a user can be obtained using Oauth Authorization Code Flow (best) or Implicit Flow (less secure). After logging in the user, obtain an access token and initialize the client with it:
import { MediaStoreApiClient } from "@biblioteksentralen/media-store-api-client";
const api = new MediaStoreApiClient({
headers: {
"user-agent": "my-little-script",
},
accessToken: `Bearer ${accessToken}`,
});
Error handling
The client throws ResponseError
on non-2XX responses and FetchError
on network errors.
A detailed error handling examples:
import {
ResponseError,
FetchError,
} from "@biblioteksentralen/media-store-api-client";
async function handleApiErrors<T>(
request: Promise<T>
): Promise<{ data?: T; error?: string }> {
try {
return { data: await request };
} catch (error: unknown) {
if (error instanceof ResponseError) {
const { status } = error.response;
return {
error: `Request failed with ${status} response: ${error.toString()}`,
};
} else if (error instanceof FetchError) {
return { error: `Request failed with network error` };
} else {
return { error: `Request failed with unknown error` };
}
}
}
const { data, error } = await handleApiErrors(api.assets.listAssets());
if (error) {
console.error(error);
} else {
console.info(data);
}
Example: React hook with SWR
export const useApi = () => {
const { session } = useSession();
return useMemo(
() =>
session.accessToken
? new MediaStoreApiClient({
basePath: "https://media.aja.bs.no/api",
userAgent: "media-store-manage",
accessToken: `Bearer ${session.accessToken}`,
})
: undefined,
[session.accessToken]
);
};
export const useListAssets = (query: string) => {
const api = useApi();
return useSWR(`listAssets:${query}`, () =>
api?.assets.listAssets(query ? { ean: query } : undefined)
);
};