Skip to content

Commit

Permalink
base_url bug solve
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikharY10 committed Oct 23, 2023
1 parent 1efbbf2 commit 47cdb72
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ A npm package that serves as utlity for directus extensions. This utility try to

# Installation

`npm install directutils`
```bash
npm install directutils
```

# Example

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
- Added JSDocs comment
- Added better logging statements
- Resolved bug with `readCollectionDataById` function

## v1.0.2

- Solved bug related to BASE_URL
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "directutils",
"version": "1.0.0",
"version": "1.0.2",
"description": "Utility for directus extensions",
"main": "./src/index.js",
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions src/lib/_internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function getBaseUrl() {
var baseURL = "";
if (process.env.ENVIRONMENT == "local") {
baseURL = process.env.DIRECTUS_LOCAL_BASE_URL;
} else if (process.env.ENVIRONMENT == "remote") {
baseURL = process.env.DIRECTUS_REMOTE_BASE_URL;
}

if (process.env.MODE == "dev") {
console.error(`[RUNNING IN ${process.env.ENVIRONMENT} ENVIRONMENT]`);
}

return baseURL;
}

module.exports = {
getBaseUrl
}
12 changes: 6 additions & 6 deletions src/lib/db_handlers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require("axios");

const { getBaseUrl } = require("./_internal");
/**
* @param {string} collection directus collection name
* @param {string} item Id of the record of a collection
Expand All @@ -8,7 +8,7 @@ const axios = require("axios");
* @returns {object} Returns collection record object
*/
async function readCollectionDataById(collection, item, query, isCustom = true) {
var url = isCustom ? `${BASE_URL}/items/${collection}/${item}?` : `${BASE_URL}/${collection}/${item}?`;
var url = isCustom ? `${getBaseUrl()}/items/${collection}/${item}?` : `${getBaseUrl()}/${collection}/${item}?`;

if (query != null) {
Object.entries(query).forEach(([key, value]) => {
Expand Down Expand Up @@ -46,7 +46,7 @@ async function readCollectionDataById(collection, item, query, isCustom = true)
* @returns {object} Returns collection read object or list based `expectMultiple` params
*/
async function readCollectionDataByQuery(collection, query, expectMultiple=false, isCustom=true) {
var url = isCustom ? `${BASE_URL}/items/${collection}?` : `${BASE_URL}/${collection}`
var url = isCustom ? `${getBaseUrl()}/items/${collection}?` : `${getBaseUrl()}/${collection}`

Object.entries(query).forEach(([key, value]) => {
if (key == "filter") {
Expand Down Expand Up @@ -90,7 +90,7 @@ async function readCollectionDataByQuery(collection, query, expectMultiple=false
* @returns {object} returns updated object
*/
async function updateCollectionDataById(collection, item, body, isCustom = true) {
const url = isCustom ? `${BASE_URL}/items/${collection}/${item}` : `${BASE_URL}/${collection}/${item}`;
const url = isCustom ? `${getBaseUrl()}/items/${collection}/${item}` : `${getBaseUrl()}/${collection}/${item}`;
const data = JSON.stringify(body);

let createPatchConfig = {
Expand Down Expand Up @@ -120,7 +120,7 @@ async function updateCollectionDataById(collection, item, body, isCustom = true)
* @returns {object} returns new created record
*/
async function createCollectionData(collection, body, isCustom = true) {
const url = isCustom ? `${BASE_URL}/items/${collection}` : `${BASE_URL}/${collection}`;
const url = isCustom ? `${getBaseUrl()}/items/${collection}` : `${getBaseUrl()}/${collection}`;
const data = JSON.stringify(body);

let createPatchConfig = {
Expand Down Expand Up @@ -149,7 +149,7 @@ async function createCollectionData(collection, body, isCustom = true) {
* @returns {boolean} returns true or false based success and failure
*/
async function deleteCollectionData(collection, itemId) {
const url = `${BASE_URL}/items/${collection}/${itemId}`;
const url = `${getBaseUrl()}/items/${collection}/${itemId}`;

let createPatchConfig = {
method: 'delete',
Expand Down

0 comments on commit 47cdb72

Please sign in to comment.