Skip to content

Commit ab63a53

Browse files
committed
Add a get primary route method.
1 parent 58545e7 commit ab63a53

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/platformsh.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ class Config {
198198
return this.routesDef;
199199
}
200200

201+
/**
202+
* Returns the primary route.
203+
*
204+
* The primary route is the one marked primary in `routes.yaml`, or else
205+
* the first non-redirect route in that file if none are marked.
206+
*
207+
* @return {object}
208+
* The route definition. The generated URL of the route is added as a "url" key.
209+
*/
210+
getPrimaryRoute() {
211+
// eslint-disable-next-line no-unused-vars
212+
for (const [url, route] of Object.entries(this.routes())) {
213+
if (route.primary === true) {
214+
return route;
215+
}
216+
}
217+
218+
throw new Error(`No primary route found. This isn't supposed to happen.`);
219+
}
220+
201221
/**
202222
* Returns a single route definition.
203223
*

test/tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ describe("Config tests", () => {
166166
});
167167
});
168168

169+
it('gets the primary route', () => {
170+
let c = new psh.Config(mockEnvironmentRuntime);
171+
172+
let route = c.getPrimaryRoute();
173+
174+
assert.equal(route['original_url'], 'https://www.{default}/');
175+
});
176+
169177
it('gets a route by id', () => {
170178
let c = new psh.Config(mockEnvironmentRuntime);
171179

0 commit comments

Comments
 (0)