Class Fdk

Main API to interact with the ec.fdk. You can create an instance with fdk.

Checkout all available methods in the sidebar on the right.

fdk("stage") // choose stage environment
.dm("83cc6374") // select datamanager via short id
.model("muffin") // select model muffin
.entryList() // load entry list
.then((list) => {
console.log(list);
});
// we want to do stuff with model muffin here
const muffin = fdk("stage").dm("83cc6374").model("muffin");
// load entry list
const { items } = await muffin.entryList();
// edit first entry
await muffin.editEntry(items[0].id, { name: "edit!" });
// delete second entry
await muffin.deleteEntry(items[1].id);
// create a new muffin
await muffin.createEntry({ name: "new muffin" });
// edit third entry with safePut
await muffin.editEntrySafe(items[2].id, {
_modified: items[2]._modified,
name: "safePut!",
});

Methods - Entries

  • Loads entry list. Expects dmShortID / model to be set. If the model is not public, you also need to provide a token.

    Parameters

    Returns Promise<EntryList>

    // public model
    const muffins = await fdk("stage").dm("83cc6374").model("muffin").entryList()
    // non-public model
    const secrets = await fdk("stage").token(token).dm("83cc6374").model("secret").entryList()
  • Maps over entry list.

    Parameters

    Returns Promise<EntryResource[]>

    // public model
    const muffins = fdk("stage").dm("83cc6374").model("muffin")
    const res = await muffin.mapEntries((entry) => muffin.editEntry(entry.id, { name: entry.name + "!" }));
    console.log("res", res);
  • Loads a single entry. Expects dmShortID / model to be set. If the model is not public, you also need to provide a token.

    Parameters

    • entryID: string

    Returns Promise<EntryResource>

    const muffin = await fdk("stage").dm("83cc6374").model("muffin").getEntry("1gOtzWvrdq")
    
  • Edits an entry with safe put. Expects dmShortID / model to be set. Expects a _modified field in the value. Will only update if the entry has not been changed since. If model PUT is not public, you also need to provide a token.

    Parameters

    • entryID: string

      id of entry to edit

    • value: object

      values to set. undefined fields are ignored

    Returns Promise<EntryResource>

    const entry = await fdk("stage")
    .dm("83cc6374")
    .model("muffin")
    .editEntrySafe("1gOtzWvrdq", { name: "test", _modified: "2020-01-01T00:00:00.000Z"})
  • Loads the schema of a model. Expects dmShortID / model to be set.

    Returns Promise<EntrySchema>

    const muffin = await fdk("stage").dm("83cc6374").model("muffin").getSchema()
    
  • Creates a new entry. Expects dmShortID / model to be set. If model POST is not public, you also need to provide a token.

    Parameters

    • value: object

      Entry value that satisfies the model's schema.

    Returns Promise<EntryResource>

    const entry = await fdk("stage").dm("83cc6374").model("muffin").createEntry({ name: 'test' })
    
  • Edits an entry. Expects dmShortID / model to be set. If model PUT is not public, you also need to provide a token.

    Parameters

    • entryID: string

      id of entry to edit

    • value: object

      values to set. undefined fields are ignored

    Returns Promise<EntryResource>

    const entry = await fdk("stage").dm("83cc6374").model("muffin").editEntry("1gOtzWvrdq", { name: "test" })
    
  • Deletes an entry. Expects dmShortID / model to be set. If model DELETE is not public, you also need to provide a token.

    Parameters

    • entryID: string

      id of entry to delete

    Returns Promise<void>

    await fdk("stage").dm("83cc6374").model("muffin").deleteEntry("1gOtzWvrdq")
    
  • Sets the given model to use

    Parameters

    • model: string

      name of the model

    Returns Fdk

  • Sets the short ID of the datamanager to use.

    Parameters

    • dmShortID: string

    Returns Fdk

  • Returns the public api root endpoint. Expects dmShortID to be set.

    Returns Promise<any>

Methods - Assets

  • Loads asset list. Expects dmShortID / assetGroup to be set. If the assetGroup is not public, you also need to provide a token.

    Parameters

    Returns Promise<AssetList>

    // public assetGroup
    const files = await fdk("stage").dm("83cc6374").assetGroup("avatars").assetList()
    // non-public assetGroup
    const files = await fdk("stage").token(token).dm("83cc6374").assetGroup("avatars").assetList()
  • Uploads an asset. Expects dmShortID / assetGroup / file to be set. If the assetGroup is not public, you also need to provide a token.

    Parameters

    Returns Promise<AssetResource>

    // browser example
    document.getElementById("file").addEventListener("input", async (e) => {
    const [file] = e.target.files;
    const asset = await ecadmin.assetgroup("test").createAsset({ file })
    });
    // node example
    const buf = fs.readFileSync("venndiagram.png");
    const file = new Blob([buf]);
    const upload = await fdk("stage")
    .dm("83cc6374")
    .assetgroup("test")
    .createAsset({ file, name: "venndiagram.png" });
  • Deletes an asset. Expects dmShortID / assetGroup / assetID to be set. You probably also need to provide a token.

    Parameters

    • assetID: string

    Returns Promise<void>

    await ecadmin.assetgroup("test").deleteAsset('xxxx');
    
  • Loads a single asset. Expects dmShortID / assetGroup to be set. If the asset group is not public, you also need to provide a token.

    Parameters

    • assetID: string

    Returns Promise<AssetResource>

    const asset = await fdk("stage").dm("83cc6374").assetgroup("test").getAsset("tP-ZxpZZTGmbPnET-wArAQ")
    
  • Sets the short ID of the datamanager to use.

    Parameters

    • dmShortID: string

    Returns Fdk

  • Sets the name of the asset group to use.

    Parameters

    • assetGroup: string

      name of the asset group

    Returns Fdk

Methods - Admin

  • Fetches resource list. Expects resource to be set. subdomain defaults to "datamanager". Fetches https://<subdomain>.entrecode.de/<resource>?_list=true&size=<options.size ?? 25>

    Parameters

    Returns Promise<ResourceList>

    const res = await fdk("stage").resource("template").resourceList()
    
  • Fetches raw route. Expects route to be set. subdomain defaults to "datamanager". Fetches https://<subdomain>.entrecode.de/<route>?<options> Use this when no other fdk method can give you your request.

    Type Parameters

    • T = any

    Parameters

    • options: object

      options that are converted to query params.

    • OptionalfetchOptions: object

      (optional) options passed to fetch.

    Returns Promise<T>

    const res = await fdk("stage").route("stats").raw()
    
  • Sets the (long) ID of the datamanager to use

    Parameters

    • dmID: any

    Returns Fdk

  • Sets the subdomain to use.

    Parameters

    • subdomain: string

      subdomain

    Returns Fdk

  • Sets the name of the resource to use.

    Parameters

    • resource: string

      name of the resource

    Returns Fdk

  • Sets the route to use.

    Parameters

    • route: string

    Returns Fdk

  • Loads model list. Expects dmID to be set. Requires auth.

    Parameters

    Returns Promise<ModelList>

    const models = await fdk("stage").dmID("254a03f1-cb76-4f1e-a52a-bbd4180ca10c").modelList()
    

Methods - Auth

  • Defines a custom storage to use for auth data. In most cases, this should not be needed. By default, auth will be stored in-memory, using a JS Map. This should be fine for NodeJS. In the Browser, it's better to use oidc-client and ignore all Auth methods here.

    Parameters

    Returns Fdk

    // using Cookies
    import Cookies from "js-cookie";
    let fdk = fdk("stage").storageAdapter(Cookies);
    await fdk.dm("83cc6374").model("my-protected-model").entryList();
  • Login with the given ec user. Only needed when using manual auth.

    Parameters

    • config: { email: string; password: string }

    Returns Promise<any>

  • Login with the given public user. Only needed when using manual auth.

    Parameters

    • config: any

    Returns Promise<any>

  • Logs out the current public user. Only needed when using manual auth.

    Returns Promise<void>

  • Logs out the current ec user. Only needed when using manual auth.

    Returns Promise<void>

  • Sets the token to use in requests. Intended for usage with a fixed token in NodeJS.

    Parameters

    • token: string

    Returns Fdk