How is MatchesPath implemented in apigee? how can we implement it in javascript?

i am trying to do similar validation as MachesPath in javascript.

I need to compare proxy.pathsuffix & pattern from variable.

Ex: pathsuffix = /a/123/users

variable= /a/{id}/users

need to compare above values matches then set some boolean value.

1 8 929
8 REPLIES 8

If I were doing this , I would use a Regular Expression. JS includes a RegExp object that allows you to perform pattern matching. The pattern language for RegExp does not use the same syntax as "/a/{id}/users", but something similar.

var r = new RegExp('^/a/([^/]+)/users$');
var s = context.getVariable('proxy.pathsuffix');
var match = s.match(r);
if (match) {
  var id = match[1]; // extract the 1st group
  ...
  context.setVariable('extracted_id', id);
}

For the above callout, if there is a match, then it sets the context variable extracted_id. If there is no match, then that variable is unset. Therefore you could use something like this in a subsequent flow:

<Step>
  <Name>RF-InvalidUrl</Name> 
  <Condition>extracted_id = null</Condition>
</Step>

For more information on regular expressions, and the pattern syntax, you can try regular-expressions.info. For an online regex tester/evaluator, try regexr. I have used that one .

Here are some example expressions:

expression explanation
^/a/([^/]+)/users$ ^ = Beginning of string
/a/ = matches itself
[^/] = any character except slash
([^/]+) = a sequence of 1 or more characters, none of which are slash
/users = matches itself
$ = end of string


^/a/([0-9]+)/users$

[0-9] = any character between 0 and 9
[0-9]+ = 1 or more characters between 0 and 9

everything else is as above.

^/a/([0-9]{5})/users$

[0-9]{5} = a sequence of exactly 5 characters, between 0 and 9

The regular expression language is very flexible; you can match just about anything you like.

thanks for your answer.

But my requirement is slightly different. I need validate path suffix dynamically & put this as common code in sharedflow. cannot add for individual proxy.

I need to store chosen paths in KVM & validate it in proxy pathsuffix. If both matches then set variable.

Did you find a solution? I would need something similar

The solution , if you want to read patterns from the KVM, is to read from KVM with the KeyValueMapOperations policy, and then reference that data in the JavaScript that I showed. 

The JS I showed used a "hard coded" pattern. Of course, you could just use context.getVariable() to retrieve a pattern you had previously loaded from KVM. 

In Apigee X, you can also set such data in Environment-scoped properties.  In that case you wouldn't need the KVM policy.  Your JS could just reference the variables from the Environment-scoped property.

@dchiesa1 

We have defined Apigee Products, and beneath each Operations on what endpoints each product should contain.
e.g. /v1/cart/**, /v1/order/*/history/*

We would like a new endpoint in Apigee to return the exact list of endpoints from proxies per product - as defined in the proxies.

We thought of the option to filter the proxy definitions through the Operations defined for a product... and use a JS equivalent of MatchesPath.
Is there a better way to do this?

We would like a new endpoint in Apigee to return the exact list of endpoints from proxies per product - as defined in the proxies.

Hmm, ok yours seems like a completely different problem. The original question is "How can I implement something like MatchesPath in JavaScript?"   It sounds to me that you are doing something ... completely different.  Maybe related to performing some administrative action - inquiring the list of ?? endpoints?? not sure...

Can you please ask a new question instead of saying "me too (except my problem is really different)" in an existing question? That "me too" with an asterisk leads to a kind of conversation diffusion, which makes it really hard for people who want to help, to understand what question they are answering, and it makes it hard for future readers to decipher just what is being answered or resolved.

When you ask a new question, be really clear about what you're asking. The word "Endpoint" in your question is not clear to me. In Apigee "endpoint" is a thing - a proxy endpoint or a target endpoint. I know people use the word differently for things outside of Apigee. But if you mean ProxyEndpoint or TargetEndpoint, use those words. Otherwise, please clarify (in the new question you ask) what you mean by "list of endpoints". There is a list of OPERATIONS defined in an API Product. Maybe that's what you mean.

operations.png

@dchiesa1 

ok, perhaps the use-case was not phrased clearly enough to clearly show where I would like to know how MatchesPath is implemented.
Allow me to rephase.

Goal: I want to create a new ProxyEndpoint that returns all the ProxyEndpoint(s) that are available for an Apigee Product.
(without calling the proxied services)

In Apigee, for a Product, one can define a set of Operations - Like in your screenshot.
See the PATH column, that has a template for a HTTP path part.
The point of the "rules" defined in the PATH column is to filter for what ProxyEndpoint(s) make up the Product.

I could create a bit of code to iterate though the list of ProxyEndpoint(s) for each proxy and do a MATCHESPATH over the Operations PATH column.

Here, would be nice either having MatchesPath available in Apigee javascript, or the rules clearly defined on how matches path was implemented so I could duplicate it.

Can you please ask a new question ?