Skip to content

Commit 5e27512

Browse files
committed
Support any Stoplight service
1 parent 3abeae4 commit 5e27512

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

lib/bundle/stoplight/generator.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ function normalizeOpts (opts) {
1414
if (typeof opts.endpointUrl === "string") {
1515
opts.endpointUrl = KeyGenerator.appendSlash(opts.endpointUrl);
1616
}
17-
else {
18-
opts.endpointUrl = null;
17+
else if (!(opts.endpointUrl instanceof RegExp)) {
18+
// {apiUrl}/api/v1/projects/{workspaceSlug}/{projectSlug}/nodes{+nodeUri}
19+
opts.endpointUrl = /\/api\/v1\/projects\/[^/]+\/[^/+]+\/nodes\/(?!$)/;
1920
}
2021

2122
return opts;
@@ -57,8 +58,10 @@ StoplightKeyGenerator.prototype.extractFilepathFromUrl = function (url) {
5758

5859
let mid = query.mid ? `_m${query.mid}` : "";
5960

60-
if (href.indexOf(endpointUrl) === 0) {
61-
return href.slice(endpointUrl.length) + mid;
61+
let [, filepath] = href.split(endpointUrl);
62+
63+
if (filepath) {
64+
return filepath + mid;
6265
}
6366

6467
return url;

test/specs/custom-bundling-roots-new-stoplight/custom-bundling-roots-new-stoplight.spec.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,39 @@ const createStoplightDefaults = require("../../../lib/bundle/stoplight/defaults"
99
const cwd = typeof window === "object" ? location.origin + "/base/test/specs/custom-bundling-roots-new-stoplight" : __dirname;
1010

1111
describe("Stoplight-specific defaults", () => {
12-
it("should recognize platform references", async () => {
13-
let defaults = createStoplightDefaults({
14-
cwd,
15-
endpointUrl: "http://jakub.stoplight-local.com:8080/api/v1/projects/jakub/my-project/nodes",
16-
});
12+
let model;
1713

14+
beforeEach(() => {
1815
setupHttpMocks({
1916
"http://jakub.stoplight-local.com:8080/api/v1/projects/jakub/my-project/nodes/reference/Book.v1.json": {
2017
title: "Book"
2118
},
19+
"http://marc.stoplight-local.com:8080/api/v1/projects/marc/my-project/nodes/models/Street.v1.json": {
20+
title: "Book"
21+
},
2222
});
2323

24-
const model = {
24+
model = {
2525
properties: {
2626
id: {
2727
type: "string"
2828
},
2929
book: {
3030
$ref: "http://jakub.stoplight-local.com:8080/api/v1/projects/jakub/my-project/nodes/reference/Book.v1.json"
3131
},
32+
street: {
33+
$ref: "http://marc.stoplight-local.com:8080/api/v1/projects/marc/my-project/nodes/models/Street.v1.json"
34+
},
3235
}
3336
};
37+
});
38+
39+
40+
it("given explicit endpointUrl, should recognize platform references pointing at that entity", async () => {
41+
let defaults = createStoplightDefaults({
42+
cwd,
43+
endpointUrl: "http://jakub.stoplight-local.com:8080/api/v1/projects/jakub/my-project/nodes",
44+
});
3445

3546
let parser = new $RefParser();
3647

@@ -51,6 +62,44 @@ describe("Stoplight-specific defaults", () => {
5162
},
5263
id: {
5364
type: "string"
65+
},
66+
street: {
67+
title: "Book"
68+
}
69+
}
70+
});
71+
});
72+
73+
it("given no explicit endpointUrl, should recognize platform references pointing at any Stoplight-compatible entity", async () => {
74+
let defaults = createStoplightDefaults({
75+
cwd,
76+
});
77+
78+
let parser = new $RefParser();
79+
80+
const schema = await parser.bundle(__dirname, model, {
81+
bundle: defaults.json_schema,
82+
});
83+
84+
expect(schema).to.equal(parser.schema);
85+
expect(schema).to.deep.equal({
86+
definitions: {
87+
"Book.v1": {
88+
title: "Book"
89+
},
90+
"Street.v1": {
91+
title: "Book"
92+
}
93+
},
94+
properties: {
95+
book: {
96+
$ref: "#/definitions/Book.v1"
97+
},
98+
id: {
99+
type: "string"
100+
},
101+
street: {
102+
$ref: "#/definitions/Street.v1"
54103
}
55104
}
56105
});

0 commit comments

Comments
 (0)