Self-Made JS API Wrapper

Heya!

I’ve been reworking my homepage and found Cockpit to be a slim yet powerful CMS. Was a little surprised to find out that there’s no API-wrapper for it, still wanted to give it a try. I’ve now implemented most of the fetching for collections and Assets in my own wrapper, including (generic) typing. Example usage:

// initialising with custom host
const api = createApi('https://cockpit.box.pasu.me/api');

// fetching all documents from a "projects" collection, including limit & sorting
// this will return a properly typed Array<Project>, where Project is a custom type
const projects = await api
  .collection<Project>(CollectionName.PROJECTS)
  .query({
    limit: 3,
    sort: {
      title: 1
    }
  });

// fetching a path to an image to include in a HTML image tag
const imagePath: string = api.image(
  // ID of image
  projects[0].image._id,
  // options passed via query params 
  {
    width: 512,
    binary: true
  }
).path;

// alternatively, you can fetch the asset with meta-data
// 'Asset' is a type provided by my wrapper, including all the meta-data returned from Cockpit
const image: Asset = await api.image(projects[0].image._id).fetch();

Currently, this is sufficient for my needs of simply displaying some blog-type stuff. If there’s any demand for it, I could certainly add the remaining features into my wrapper and publish it as a npm package.

If you want to check my code:

Let me know what you think and whether you’d want to use this yourself!

Many greets from Germany
Pasu

Little update - I was bored and actually packed my little client into its own package and published it to npm (with its own repository)!

I’ll continue implementing the remaining features and updating the documentation.