Socket
Socket
Sign inDemoInstall

@adoratorio/demetra

Package Overview
Dependencies
10
Maintainers
4
Versions
71
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @adoratorio/demetra

Internal use library for wordpress API request


Version published
Weekly downloads
5
decreased by-77.27%
Maintainers
4
Created
Weekly downloads
 

Readme

Source

Demetra

A utility library for WordPress custom API interaction, using the adora-theme endpoint.

Installation

# Install package
npm install @adoratorio/demetra

Usage

Since this package has a pkg.module field, it’s highly recommended importing it as an ES6 module with some bundlers like Webpack or Rollup:

import Demetra from '@adoratorio/demetra';
const demetra = new Demetra(options: Object);

Available options

Demetra accepts in the constructor an option object with the following possible props.

parametertypedefaultdescription
endpointstring''The URL for the custom theme API endpoint
uploadEndpointstring{$endpoint}/upload.phpThe URL for the custom theme API endpoint for uploading files
sitestring'default'In the Wordpress multi-site installation the site id to fetch data from
langstring'en'The language in which data is retrieved
debugbooleanfalseIf You need extra log in browser console about what Demetra is doing
versionnumber2The API version used (V2 is now available!)
cacheMaxAgenumber3600000Maximum cache age in ms. If the request will use the local cache (LRU Cache)
proxyAxiosProxyConfigfalseAn http/https proxy to use for requests described as axios proxy config object

APIs

fetchPage()

Use this method to get pages details, the method accept the following params

Demetra.fetchPage(id : string | number, options : object);

Accepted parameters

parameterrequireddescription
idtrueThe id or the slug of the page to fetch
optionsfalseThe configuration object

Options can take an object with the following keys

parametertypedefaultdescription
typestring'page'The custom post type id or 'page' if you need an actual page not a post.
siblingsobject{ fields: [], prev: false, next: false, loop: false }If you also need information about adjacent siblings
wpCachebooleantrueIf the endpoint will use the API cache
localCachebooleanfalseIf you want to save the data in a local cache (LruCache)
langstringDemetra.langThe language in which data is retrieved
i18nbooleantrueIf you need to get the i18n object in the response containing all information about the other languages available for this page

Siblings can take an object with the following keys

parametertypedefaultdescription
fieldsArray[]An array of fields you need for siblings, identified by their frontId
prevbooleanfalseIf you need the prev sibling
nextbooleanfalseIf you need the next sibling
loopBooleanfalseIf the requested page is the last or the first, treat the siblings as a circle, returning the first one or the previous one depending on the position

The returned object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded!",
    "cache": false
  },
  "data": {
    "id": 1,
    "title": "Homepage",
    "path": "/homepage/",
    "fullPath": "/en/homepage/",
    "slug": "homepage",
    "structure": [
      {
        "frontId": "Component Name"
        // ... Other item data
      }
        // ... Other item components
    ],
    "lang": "en",
    "type": "page",
    "i18n": {
      "it": {
        "id": 111,
        "slug": "homepage",
        "path": "/homepage",
        "fullPath": "/it/homepage"
      }
    },
    "siblings": {
      "next": {
        "id": 222,
          "title": "Siblings Title",
          "path": "/sibligns-path/",
          "fullPath": "/en/sibligns-path/",
          "structure": [
            false
          ],
          "lang": null,
          "type": "page",
          "i18n": null,
          "siblings": null,
          "date": null
        },
        "prev": null
    },
    "date": null // Filled only wen the requested type is a post
  }
}

fetchArchive()

Retrieve the information and content for an archive (a collection of items)

Demetra.fetchArchive(id: string, options : object);

Accepted parameters

parameterrequireddescription
idtrueThe slug of the archive to fetch
optionsfalseThe configuration object

Options can take an object with the following keys

parametertypedefaultdescription
fieldsstring[]Array of frontId used to identify the fields for the items
paginationobject{ start: 0, count: -1 }A pagination object used to define if you need pagination
wpCachebooleantrueIf the endpoint will use the API cache
localCachebooleanfalseIf you want to save the data in a local cache (LruCache)
langstringDemetra.langThe language in which data is retrieved
i18nbooleantrueIf you need to get in response the i18n object containing all the information about the other available languages for this page

Pagination can take an object with the following keys

parametertypedefaultdescription
startnumber0
countnumber-1

The returning object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded!",
    "cache": false
  },
  "data": {
    "items": [
      {
        "id": 111,
        "title": "First Article",
        "path": "/first-article/",
        "fullPath": "/en/first-article/",
        "structure": [
          {
            "frontId": "Component Name",
            // ... Other item data
          }
          // ... Other item components
        ],
        "lang": "en",
        "type": "press",
        "i18n": null,
        "siblings": null,
        "date": null
      }
    ],
    "pagination": {
      "start": 0,
      "count": -1,
      "more": true,
      "total": 7
    }
  }
}

fetchExtra()

Fetch data considered to be extra in the Wordpress setup

Demetra.fetchExtra(id: string, options? : object);

Accepted parameters

parameterrequireddescription
idtrueThe slug of the extra to fetch
optionsfalseThe configuration object

Options can take an object with the following keys

parametertypedefaultdescription
wpCachebooleantrueIf the endpoint will use the API cache
localCachebooleanfalseIf you want to save the data in a local cache (LruCache)
langstringDemetra.langThe language in which data is retrieved
i18nbooleantrueIf you need to get in response the i18n object containing all the information about the other available languages for this page

The returning object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded!",
    "cache": false
  },
  "data": {
    "privacy": "",
    "cookies": "",
    "email": ""
    // ... Extra data with key : value here
  }
}

fetchMenu()

Fetch content for a menu

Demetra.fetchMenu(id: string, options : object);

Accepted params

parameterrequireddescription
idtrueThe slug of the menu to fetch
optionsfalseThe configuration object

Options can take an object with the following keys

parametertypedefaultdescription
wpCachebooleantrueIf the endpoint will use the API cache
localCachebooleanfalseIf you want to save the data in a local cache (LruCache)
langstringDemetra.langThe language in which data is retrieved
i18nbooleantrueIf you need to get in response the i18n object containing all the information about the other available languages for this page

The returned object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded!",
    "cache": false
  },
  "data": {
    "items": [
      {
        "caption": "Home",
        "link": {
          "title": "Home",
          "url": "https://xyz/about/",
          "target": ""
        }
      }
      // ... Other menu item
    ]
  }
}

fetchTaxonomy()

Fetch a single taxonomy with all terms

Demetra.fetchTaxonomy(id: string | [string], options? : object);

Accepted parameters

parameterrequireddescription
idtrueThe slug or an array of slug of the taxonomy to fetch
optionsfalseThe configuration object

Options can take an object with the following keys

parametertypedefaultdescription
wpCachebooleantrueIf the endpoint will use the API cache
localCachebooleanfalseIf you want to save the data in a local cache (LruCache)
langstringDemetra.langThe language in which data is retrieved
i18nbooleantrueIf you need to get in response the i18n object containing all the information about the other available languages for this page

The returning object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded!",
    "cache": false
  },
  "data": {
    "items": [
      {
        "term_id": 1,
        "name": "TERM NAME",
        "slug": "termname",
        "term_group": 0,
        "term_taxonomy_id": 1,
        "taxonomy": "taxonomy id",
        "description": "",
        "parent": 0,
        "count": 0,
        "filter": "raw"
      }
      // ... Other terms following
    ]
  }  
}

fetchLanguages()

Use this method to get locales details such as:

  • code
  • iso
  • default_locale
  • name
  • translated_name

the method accept the following params

Demetra.fetchLanguages(site : string, options : object);

Accepted parameters

parameterrequireddescription
sitetruethe id of the reference site
optionsfalseThe configuration object

Options can take an object with the following keys

parametertypedefaultdescription
wpCachebooleantrueIf the endpoint will use the API cache
localCachebooleanfalseIf you want to save the data in a local cache (LruCache)
langstringDemetra.langThe language in which data is retrieved

The returned object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded",
    "cache": false
  },
  "data": {
    "locales": [
      {
        "code": "en",
        "iso": "en",
        "default_locale": "en_US",
        "name": "English",
        "translated_name": "Inglese",
        "hidden": false
      },
      {
        "code": "it",
        "iso": "it",
        "default_locale": "it_IT",
        "name": "Italiano",
        "translated_name": "Italiano",
        "hidden": false
      }
    ],
    "defaultLocale": "en"
  }
}

fetchSitemap()

Use this method to get a list with all the project route.

Demetra.fetchSitemap(site : string, options : object);

Accepted parameters

parameterrequireddescription
sitetruethe id of the reference site
optionsfalseThe configuration object

Options can take an object with the following keys

parametertypedefaultdescription
wpCachebooleantrueIf the endpoint will use the API cache
localCachebooleanfalseIf you want to save the data in a local cache (LruCache)

The returned object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded",
    "cache": false
  },
  "data": [
    "/explore/",
    "/about/",
    "/dining/bar-aperitifs/",
    "/dining/breakfast/",
    "/dining/restaurants/second-restaurant/",
    "/dining/restaurants/first-restaurant/",
    "/dining/",
    "/suites/second-suite/",
    "/suites/first-suite/",
    "/suites/",
    "/",
    "/it/explore/",
    "/it/about/",
    "/it/pasti/bar-aperitifs/",
    "/it/pasti/breakfast/",
    "/it/pasti/restaurants/second-restaurant/",
    "/it/pasti/restaurants/first-restaurant/",
    "/it/pasti/",
    "/camere/second-suite/",
    "/camere/first-suite/",
    "/camere/",
    "/it/home"
  ]
}

fetchAttachments()

Use this method to get a list with all the project route.

Demetra.fetchAttachments(site : string, options : object);

Accepted parameters

parameterrequireddescription
sitetruethe id of the reference site
optionsfalseThe configuration object

Options can take an object with the following keys

parametertypedefaultdescription
wpCachebooleantrueIf the endpoint will use the API cache
localCachebooleanfalseIf you want to save the data in a local cache (LruCache)

The returned object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded",
    "cache": false
  },
  "data": [
    "/.../",
  ]
}

subscribe()

Use configured MailChimp settings in order to subscribe an email to a list

Demetra.subscribe(email : string);

Accepted parameters

parameterrequireddescription
emailtrueThe e-mail to subscribe to the newsletter

The returning object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded!",
    "cache": false
  },
  "data": {
    "response": 1, // The request result 0 in case of error
    "message": "SUCCESS!", // The message associated with the result
    "mailchimp": "", // Contains mailchimp messages and debug informations
  },  
}

send()

Use a preconfigured form on WP to send an email

Demetra.send(id : number, recipients : string, data : object, files : array);

Accepted parameters

parameterrequireddescription
idtruethe form ID
recipientstrueshould be a single email, or a comma separated list of email addresses
datatruedata should be a valid JSON parsable string, containing only one level key/value pairs, one for each field defined in form options on WP side
filesfalseshould be a list of files handler to upload as attachments to the reques

The returning object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Data loaded!",
    "cache": false
  },
  "data": {
    "saved": 1, // How many requests have been saved in the db
    "sended": true // If the email has been sended to the recipients
  }
}

upload()

Upload one or multiple files to the Wordpress media library

Demetra.upload(files : file | array<file>);

The returning object will be in the following form

{
  "status": {
    "code": 200,
    "message": "Upload successful",
  },
  "data": [
    {
      "upload_id": 1, // The post id of the uploaded file
      "url": "https://..." // The url of the uploaded file
    }
  ]  
}

fetchQueue()

Fetch all the DemetraRequest in the queue in three different ways: all together, simultaneously or one at time.

fetchQueue(sendModes: string)

Accepted parameters

parametertyperequireddefaultdescription
idstringfalseDemetra.SEND_MODES.ONCEA string indicating the send mode you wish to use, it can be 'once', 'simultaneously' or 'await'. Also static enumerators are exposed to help:
Demetra.MODE.ONCE
Demetra.MODE.SIMULTANEOUSLY
Demetra.MODE.AWAIT.

Modes:

  • Once: create a request package and send thath package.
  • Simultaneously: simultaneously sends all requests independently of each other
  • Await: Wait a response before send the next request

Queue

Internally Demetra uses a request queue which is used together with the fetchQueue() function

add()

Adds a request to the queue.

Demetra.add(request : DemetraRequestPage | DemetraRequestArchive | DemetraRequestExtra | DemetraRequestMenu | DemetraRequestTaxonomy)

Clear()

Clear the queue.

Demetra.clear()

DemetraRequests

In V2 you can directly create a DemetraRequest thath can be directly sent via fetchQueue() . You can instantiate one or more of the following class:

  • DemetraRequestArchive(id : string | number, options : object, lang : string, site : string, version : number)
  • DemetraRequestExtra(id : string | number, options : object, lang : string, site : string, version : number)
  • DemetraRequestMenu(id : string | number, options : object, lang : string, site : string, version : number)
  • DemetraRequestPage(id : string | number, options : object, lang : string, site : string, version : number)
  • DemetraRequestTaxonomy(id : string | number, options : object, lang : string, site : string, version : number)
  • DemetraRequestLanguages(site : string, options : object, lang : string, version : number)
  • DemetraRequestSiteMap(site : string, options : object, version : number)
  • DemetraRequestAttachments(site : string, options : object, version : number)

NB: The request doesn't inherit the global parameters of Demetra. Tho you can pass the lang, site and version params directly in the options

Accepted parameters

parametertyperequireddefaultdescription
idstring | numbertrueThe slug or id of the archive | extra | page | menu | taxonomy to fetch
optionsobjectfalseThe configuration object (look the fetch API above to understand how to fill the object for each request)
langstringfalse'en'The language in which data is retrieved
sitestringfalse'default'In multi-site installation Wordpress the site id to fetch data from
versionnumberfalse2The API version used

Keywords

FAQs

Last updated on 20 Nov 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc