Passing path parameters in axios - node.js

I am using Axios with NodeJs and trying to pass path parameters in axios.get() method. For example, if URL is url = '/fetch/{date}', I want to replace {date} with the actual date while calling axios.get(url).
I went through the source code on Github and StackOverflow, but couldn't find any method.
Is it possible to keep URLs with parameters as a placeholder and replace them while actually calling the get method of Axios?

Axios doesn't have this feature and it looks like the team don't want to add it.
With credit to previous responders for inspiration, to me this seems like the solution closest to what you (and me) are looking for:
1 - Where you want to store all your URLs and their parameters, define them as functions which use a template string to return the composed URL:
export var fetchDateUrl = (date) => `/fetch/${date}`;
If you need any type-specific formatting of the value being concatenated into the URL, this function is a good place to do it.
2 - Where you want to make the request, call the function with the correct parameters:
import { fetchDateUrl } from 'my-urls';
axios.get(fetchDateUrl(someDateVariable))...;
Another variation, if you really like the idea of naming the parameters at the call site, you can define the URL function to destructure an object like this:
var fetchDateUrl = ({date}) => `/fetch/${date}`;
which you'd then use like this:
axios.get(fetchDateUrl({date: someDateVariable}));

Use template strings
url = `/fetch/${date}`
Or just tag it on
url = '/fetch/'+ date

I think using axios interceptors is better to do this :
//create your instance
const instanceAxios = axios.create({
baseUrl: 'http://localhost:3001'
]);
instanceAxios.interceptors.request.use(config => {
if (!config.url) {
return config;
}
const currentUrl = new URL(config.url, config.baseURL);
// parse pathName to implement variables
Object.entries(config.urlParams || {}).forEach(([
k,
v,
]) => {
currentUrl.pathname = currentUrl.pathname.replace(`:${k}`, encodeURIComponent(v));
});
const authPart = currentUrl.username && currentUrl.password ? `${currentUrl.username}:${currentUrl.password}` : '';
return {
...config,
baseURL: `${currentUrl.protocol}//${authPart}${currentUrl.host}`,
url: currentUrl.pathname,
};
});
// use like :
instanceAxios.get('/issues/:uuid', {
urlParams : {
uuid: '123456789'
}
})
For typescript users, you will need to add this, in one of your .d.ts
declare module 'axios' {
interface AxiosRequestConfig {
urlParams?: Record<string, string>;
}
}
( this is a POC, not really tested, doesn't hesitate if you see something wrong )

You can use template strings ie:
let sellerId = 317737
function getSellerAnalyticsTotals() {
return axios.get(`http://localhost:8000/api/v1/seller/${sellerId}/analytics`);
}

Given some API /fetch/${date} you likely want to wrap your axios call in a function.
const fetchData = (date) => axios.get(`/fetch/${date}`);
fetchData(dateObject.toFormat('yyyy-mm-dd'))
.then(result => { ... });
This requires the calling code to format date correctly however. You can avoid this by using a DateTime library that handles date string parsing and do the format enforcement in the function.
const fetchData = (date) => axios.get(`/fetch/${date.toFormat('yyyy-mm-dd')}`);
fetchData(dateObject)
.then(result => { ... });

you can do like this:
getProduct = (id) => axios.get(`product/${id}`);

I always do it like this:
const res = await axios.get('https://localhost:3000/get', { params: { myParam: 123 } });
I find this to be much clearer than template strings.
More explanation here

Related

Nestjs & TypeOrm: No results from Query Builder using getOne() / getMany()

I don't get this. I have a service that injects entity repositories and has dedicated methods to do some business logic and functions.
Beside that I expose a method that just returns QueryBuilder - to avoid injecting repositories all over the place - for a few occasions when other service needs just a quick query:
type EntityFields = keyof MyEntity;
entityQueryBuilder(alias?: string, id?: number, ...select: EntityFields[]) {
const q = this.entityRepository.createQueryBuilder(alias);
if (id) {
q.where({id});
}
if (select) {
q.select(select);
}
return q;
}
Now when I am trying to use this and call:
const r = await service.entityQueryBuilder('a', 1, 'settings').getOne();
the result is always empty although in the log the generated SQL is correct.
However when I do:
const r = await service.entityQueryBuilder('a', 1, 'settings').execute();
I get (almost) what I need. I get array instead of an entity object directly but the data are there.
I am unhappy though as I need to map the result to the object I wanted, which is something that getOne() should do on my behalf. getMany() does not return results either.
What did I do wrong?
Edit:
FWIW here is the final solution I came up with based on the hint in accepted reply:
entityQueryBuilder(id?: number, ...select: EntityFields[]) {
const q = this.entityRepository.createQueryBuilder('alias');
if (id) {
q.where({id});
}
if (select) {
q.select(select.map(f => `alias.${f}`));
}
return q;
}
Admittedly it has hardcoded alias but that I can live with and is OK for my purpose.
Hope this helps someone in the future.
It happens because you put no really proper select. In your case, you need a.settings instead of settings:
const r = await service.entityQueryBuilder('a', 1, 'a.settings').getOne(); // it should works

how to use shift on behaviorSubject?

I have a class with BehaviorSubject:
export class WordsService {
private words = new BehaviorSubject<WordType[]>([]);
It was fed with subscription:
init() {
this.databaseService.fetchWords().subscribe(
(listaWords: WordType[]) => {
this.words.next(listaWords);
},
errors => console.error('err'),
() => console.log('suceed')
)
}
And as I'm refactoring this code:
private fetchWord(): void{
this.word = this.wordService.getWords().shift();
}
I'm trying to get variable word to have data with .shift so it can take one element from observable at once, and when it will take all elements fetching's done.
It looks like you are trying to transform the results from a WordType[] to a single WordType.
You can do this by applying the map() operator like this:
init(){
this.databaseService.fetchWords().pipe(
map(words => words.shift())
)
.subscribe(
(listaWords: WordType[]) => {
this.words.next(listaWords);
},
errors => console.error('err'),
() => console.log('suceed')
)
}
However, you don't actually need a BehaviorSubject to do this, you can simply declare your observable directly from your service call:
public word: Observable<WordType> = this.databaseService.fetchWords().pipe(
map(words => words.shift()),
catchError(error => console.log(error))
);
Now, the word observable will only emit the value you are interested in.
This allows you to possibly use the async pipe in your template to manage the subscription and not need to do it yourself in the controller.
I would do it without shifting.
Imagine you have any observable value, for each emition of this observable value you want to pull a word. In my example this observable value is a page click.
Then you can do something like this:
const clicked$ = fromEvent(document, 'click');
const words$ = of(['AA', 'BB', 'CC', 'XX']);
const wordToPrint$ = zip(
clicked$,
words$.pipe(concatAll()),
).pipe(
map(([,word]) => word),
);
wordToPrint$.subscribe(console.log);
See stackblitz: https://stackblitz.com/edit/rxjs-ep1k3v?file=index.ts

Getting image URL from Contentful entry id

I need to get an image URL from Contentful entry id.
I am getting such an JSON from Contentful query
{
"sys":{
"space":{
"sys":{
"type":"Link",
"linkType":"Space",
"id":"8v1e7eaw70p2"
}
},
"id":"1JfEwVlD9WmYikE8kS8iCA",
"type":"Entry",
"createdAt":"2018-02-28T18:50:08.758Z",
"updatedAt":"2018-02-28T18:50:08.758Z",
"revision":1,
"contentType":{
"sys":{
"type":"Link",
"linkType":"ContentType",
"id":"image"
}
},
"locale":"en-US"
},
"fields":{
"name":"heat",
"image":{
"sys":{
"type":"Link",
"linkType":"Asset",
"id":"6Inruq2U0M2kOYsSAu8Ywk"
}
}
}
}
I am using JS driver they provide:
client.getEntry()
so how to go thru that link: 6Inruq2U0M2kOYsSAu8Ywk ?
Unfortunately, the js SDK will not be able to resolve links when using the single entry endpoint i.e client.getEntry() because there won't be enough data.
When thing I always recommend to work around this is to use the collection endpoint with a query the desired id as a query param. This way you will always get the desired entry with all it's linked data.
Your code should look something like this
client.getEntries({'sys.id': '6Inruq2U0M2kOYsSAu8Ywk'})
.then(response => console.log(response.items[0].fields.image.fields.file.url))
I hope that helps.
Best,
Khaled
Use client.getEntries({'sys.id': '1JfEwVlD9WmYikE8kS8iCA'})
To get the entry fields and the asset fields.
You can also patch the assets to the fields by running this after fetching the data:
/* Patch all the assets to the fields */
const patchAssets = (fields, assets) => {
Object.keys(fields).forEach(function (key) {
let obj = fields[key];
if (obj.sys && obj.sys.linkType === 'Asset') {
const assetId = obj.sys.id;
const matchAsset = assets.find(asset => {
return asset.id === assetId;
});
obj.file = matchAsset;
}
});
return fields;
};
Another way to get image url is to use getAsset('<asset_id>'). So first, using the getEntry() method, you need to get the entry data, then extract the id from the field: fields.image.sys.id, and pass it to the getAsset method.

How Express routes similar url links?

Developing web app with node.js and express.
I have following two urls to distinguish:
/api/v1/source?id=122323
/api/v1/source?timestamp=1555050505&count=10
I come up a naive solution. I leave such similar urls to one route method and use if eles to specify solutions, i.e:
if(id){
//solution with id
}
if(timestamp&&count){
//solution with timestamp and count but without id
}
Apparently, this is not clean. Because in the future,I may want to add new field which will make this router huge and ugly.
So How can I overcome this? Or to change url structure.I want to build a Restful api.
Try to put together all the properties in a list and use Array#every to check if all the values in Array evaluates to true.
Maybe something like this:
(( /* req, res */)=>{
// Dummy express Request Object
const req = {
params : {
//id : '123',
count : 10,
timestamp : 1555050505,
newParameter : 'whatever value'
}
}
let { params } = req;
let {
id
, count
, timestamp
, newParameter
} = params;
if(id){
console.log('Action with id');
return;
}
let secondConditionArray = [
count, timestamp, newParameter
];
if( secondConditionArray.every(Boolean) ){
console.log('Second Action')
} else {
console.log('Some values are no truthy')
}
})()
You can get Url parameters with req.params
if(req.params.id){
//solution with id
}
if(req.params.timestamp && req.params.count){
//solution with timestamp and count but without id
}

I need to create url for get which is going to accept array, how in node.js/express extract array from request?

I need to create url for get which is going to accept array, how in node.js/express extract array from request ?
I need to pass array with names which parametes I need to back from Person
model.
/api/person # here I need to pass which fields I want to see but to be generic.
One option is using a JSON format.
http://server/url?array=["foo","bar"]
Server side
var arr = JSON.parse(req.query.array);
Or your own format
http://server/url?array=foo,bar
Server side
var arr = req.query.array.split(',');
Express exposes the query parameter as an array when it is repeated more than once in the request URL:
app.get('/', function(req, res, next) {
console.log(req.query.a)
res.send(200)
}
GET /?a=x&a=y&a=z:
// query.a is ['x', 'y', 'z']
Same applies for req.body in other methods.
You can encode an array in percent encoding just "overwriting" a field, formally concatenating the values.
app.get('/test', function(req,res){
console.log(req.query.array);
res.send(200);
});
localhost:3000/test?array=a&array=b&array=c
This query will print ['a','b','c'].
Using next code:
app.use('/', (req, res) => {
console.log(req.query, typeof req.query.foo, Array.isArray(req.query.foo));
res.send('done');
});
On backend, you have two standard approaches. For next requests:
/?foo=1&foo=2
/?foo[]=1&foo[]=2
your NodeJS backend will receive next query object:
{ foo: [ '1', '2' ] } 'object' true
{ foo: [ '1', '2' ] } 'object' true
So, you can choose the way you want to. My recommendation is the second one, why? If you're expect an array and you just pass a single value, then option one will interpret it as a regular value (string) and no an array.
[I said we have two standards and is not ok, there is no standard for arrays in urls, these are two common ways that exist. Each web server does it in it's own way like Apache, JBoss, Nginx, etc]
If you want to pass an array from url parameters, you need to follow the bellow example:
Url example:
https://website.com/example?myarray[]=136129&myarray[]=137794&myarray[]=137792
To retrieve it from express:
console.log(req.query.myarray)
[ '136129', '137794', '137792' ]
Express has a tool to check if your path will match the route you are creating : Express.js route tester.
As Jose Mato says you have to decide how to structure your url:
?foo=1&foo=2
?foo[]=1&foo[]=2
The http request should look like this, if you chose method 1:
http://baseurl/api/?foo=1&foo=2
Your route should have this logic:
app.get('/api/:person', (req, res) => {
/*This will create an object that you can iterate over.*/
for (let foo of req.params.foo) {
/*Do some logic here*/
}
});
You can pass array elements separated by slashes -
GET /api/person/foo/bar/...
Define your route as '/api/person/(:arr)*'
req.params.arr will have the first parameter.
req.params[0] will have the rest as string.
You split and create an array with these two.
app.get('/api/person/(:arr)*', function(req, res) {
var params = [req.params.arr].concat(req.params[0].split('/').slice(1));
...
});
GET /api/person/foo
params = ["foo"]
GET /api/person/foo/bar
params = ["foo", "bar"]
...
Here use this, '%2C' is the HTML encoding character for a comma.
jsonToQueryString: function (data) {
return Object.keys(data).map((key) => {
if (Array.isArray(data[key])) {
return encodeURIComponent(`${key}=${data[key].map((item) => item).join('%2C')}`);
}
return encodeURIComponent(`${key}=${data[key]}`);
}).join('&');
}
To access the query params
const names = decodeURIComponent(req.query.query_param_name);
const resultSplit = names.split(',');
Theres a problem when the array param contains only one value, because then it behaves like a simple string. Or when it doesn't contain any value.
Eg.:
?arrayParam=5
?
?arrayParam=5&arrayParam=6
Here's a function that always extracts the param with the given name, as an array:
export function extractArrQueryParams(
req: express.Request,
paramName: string
) {
let param = req.query[paramName];
let returnArray = [];
if (Array.isArray(param)) {
for (let e of param) {
returnArray.push(e);
}
} else {
if (param) {
returnArray.push(param);
}
}
return returnArray;
}

Resources