SpoonX/aurelia-api

Allow the resource path to contain query parameters

Closed this issue · 6 comments

In several setups, I don't have to possibility to activate rewriting for ReSTful API pathes, so I have to setup the API endpoint like following:

this.fetchClient.baseUrl = backend.basepath+'/api/index.php?_p=';

But this fails when trying to find() in aurelia-orm with criteria:

this.assetRepository.find({criteria:1}).then(assets=>{
    // assets
});

requests to http://dev-server/api/index.php?_p=/assets?criteria=1

Its propable better to use url-parse for creating/generating URLs.

import URL from 'url-parse';

function getRequestPath(resource: string, traditional: boolean, idOrCriteria?: string|number|{}, criteria?: {}) {
	let resourcePath = new URL(resource,true);
	let hasSlash = resourcePath.pathname.slice(-1) === '/';
	
	if (typeof idOrCriteria === 'string' || typeof idOrCriteria === 'number') {
		criteria = idOrCriteria
	}
	
	if (typeof criteria === 'object' && criteria !== null) {
		for (let key in criteria) {
			resourcePath.query[key] = criteria[key];
		}
	} else {
		resourcePath.pathname+=(hasSlash === false ? '/' : '')+criteria;
	}
	return resourcePath.toString();
}

PRO: its cleaner and much more easier to read.
CON: traditional parameter currently not implemented.

I personally don't want to do that. I think this is a very specific problem, and maybe it should be handled differently. What we can do is search for a question mark in the base URL, and use either ampersand or question mark based on that result.

This is a very common problem in PHP environments, where you need to enable rewriting on the webserver.

@mreiche IMO, It's a very outdated problem to have... But that's not relevant to this issue :)

Im working on projects with legacy code older than 13 years. This is an outdated but still present problem.

@mreiche I don't envy you :p