I'm tring to filter a route with param, but no success. I have 2 routes:
/all
/:id
I'm using typescript-rest to dynamic load, so there's no way to add the all route first.
Server.loadServices(this.appWebApi, 'app/resources/*.js', `${__dirname}`);
I have this 2 methods:
#GET
#Path("all")
async all(#Context context: ServiceContext) {}
#GET
#Path(":id")
async findById(#Context context: ServiceContext, #PathParam("id") id: string) {}
But both is called when i use /all. I tried to use ^(?!all):id but the parameter is not working.
So, how to filter this 2 routes?
I found a solution, the problem is related to typescript-rest package.
Here the proposed solution:
https://github.com/thiagobustamante/typescript-rest/issues/78#issuecomment-469232775
Related
I have a route (route-a) that transitions to another route (route-b) and I am trying to find a way for the destination URL to maintain the all query parameters, even if route-b does not know about them in advance.
For example, if a user visits https://example.com/route-a/?var1=x&var2=y, and the transition to route-b happens like this:
afterModel(model, transition) {
this.transitionTo('route-b', model, {queryParams: transition.to.queryParams}) // transition route-a to route-b
}
...the ultimate URL will be https://example.com/route-b/ — without the query params.
Now, I realize the "Ember way" is to define the queryParams on route-b's controller in advance, but in this particular use-case, I do not know the queryParams in advance. Route B consumes any and all query params provided to it, which means they would be impossible to enumerate in advance.
How can I transition to a new route without dropping query parameters that are not specifically enumerated on the destination route's controller?
Is there a way to handle unknown queryParams, or is there the notion of a wildcard for queryParams (similar to *path routes)?
Update: I'm not marking this as the answer, because as jelhan notes below, using a computed property for this key is explicitly identified as a no-no in the docs. But it worked for our use-case, and it might for others, though I'm guessing it may break down if you have additional queryParams in other routes that might conflict when Ember attempts to combine them.
Previous answer:
My solution here ended up using Ember's computed method to auto-generate the Array of query params by parsing the URL.
queryParams: computed("router.location", function () {
let qp = this.get("router.location").getURL().split("?")[1];
if (qp) {
let qpAsObj = JSON.parse(
'{"' +
decodeURI(qp)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}'
);
return Object.keys(qpAsObj)
}
})
If you don't want to subsequently maintain those query params on the page/model the next time a user re-visits that page ("sticky query params"), you will also need to remove the queryParams on the route:
resetController(controller) {
// unset all queryParams when leaving the route
controller.queryParams.forEach(v => {
controller.set(v, null)
})
}
This solution is... not ideal, but it works and we have tests written to ensure that we will catch any errors if it breaks going forward.
I am started with nestjs recently and i want apply a middleware for all routes, except the auth route.
In the documentation it says that I can add all the controllers or paths of the routes where I want the middleware, but I find this way quite expensive
.forRoutes(
SearchController,
Other,
Other,
Other
);
So I would like to know if there is a solution, or if you managed to use regex for something like this:
.forRoutes(
{path: 'All route except /auth', method: RequestMethod.ALL}
);
Nowadays we use the function exclude.
export class YourModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(YourMiddleware)
.exclude('auth')
.forRoutes('*');
}
}
For further: https://docs.nestjs.com/middleware#excluding-routes
DOC: https://docs.nestjs.com/middleware
TITLE: Excluding routes
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggerMiddleware).forRoutes('*')
consumer.apply(tokenVerify).exclude('/auth').forRoutes('*')
} }
Use /auth/(.*) in case you have routes like /auth/login, /auth/register, etc. .
Exclude route referring exact route path.If you have define global prefix at main.ts,It's also need to come in to that exclude path.
Eg :
main.ts
app.setGlobalPrefix('api/v1');
module.ts
export class YourModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(YourMiddleware)
.exclude({ path:'api/v1/auth',method: RequestMethod.ALL})
.forRoutes('*');
}
}
Does Ember expose an API I can use to manually look up a route (as a dot separated path) from a URL?
For example:
Ember.routeForURL('/foo/bar')
There is no public API for that as far as I know. What you can do is use router's recognizer.
let owner = Ember.getOwner(this);
let router = owner.lookup('router:main');
let handlers = router.router.recognizer.recognize('/foo/bar')
"handlers" will contain an array of objects where handler property will be something like this ["application", "foo", "foo.bar"]
And then you can probably use the last handler to do
owner.lookup('route:' + lastHandler)
I need determine the route name from a URL string.
This is normally work that the Router does internally, but because of {insert long story here} I need to do it manually. I am given a piece of data from the API that looks like 'gallery/123' and I need to know the route name is 'gallery.post'. So that I can do route.replaceWith(determinedRouteName);
Need to turn 'gallery/123' into 'gallery.post'
Need to turn 'stuff/99/comments' into 'stuff.post.comments'
Etc
Ember relies internally to the router-recognizer micro lib. I think that your best option is to use it as well.
Unfortunately, accessing the router instance is currently requiring to rely on the private -routing service. (Note: here is a pending RFC about offering a public router service).
Injecting this private service:
import Ember from 'ember';
export default Ember.Route.extend({
router: Ember.inject.service('-routing'),
...
});
The recognize function returns the list of all handlers that you can join if you want to build the complete string or anything else.
Ex:
this.router.recognize("stuffs/99/comments");
> [{handler: "application", ...},
handler: "stuffs", ...},
handler: "stuff", ...},
handler: "comments", ...},
handler: "comments.index, ...}]
Hope it helps
Are you going to always only have one ID in the route? Or will the first ID always be the 'post' ID at least? If so you this isn't really an Ember issue it would just be a JavaScript find/replace that always follows the same format assuming the API data always come back in the same format.
Assuming the API always provides this format:
"something/:post_id/optionally_something_else"
You could take the string that the API provided and run it through a couple replace methods:
var apiString = "stuff/99/comments";
apiString = apiString.replace(/[0-9]+/, "post").replace(/\//g, "."); // => "stuff.post.comments"
// Also works with:
var apiString = "gallery/123";
apiString = apiString.replace(/[0-9]+/, "post").replace(/\//g, "."); // => "gallery.post"
DISCLAIMER: This is pretty icky and will break if the string doesn't follow this format. It will also only covert the first set of digits to "post". But as usual, use it if you must :P
I need to add a route for the following syntax:
http://www.testsite.com/select?term=query1
In my routes file, I tried using the following
GET /select/{term}
However, the above does not catch the URL - instead it goes to another handler in the config (placed beneath the handler for select/{term}:
GET /{auth}
Any thoughts on fixing or troubleshooting this would be most welcome. thanks
?term= means that term is a parameter - not part of the route you are trying to match
so you'd write
GET /select YourControllerClass.yourMethod
....
YourControllerClass extends Controller {
public static void yourMethod(String term){
Logger.debug("term=" + term);
}
}
If your URL was http://www.testsite.com/select/query1 then the route definition you provided above should work