Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove previews logic #388

Merged
merged 14 commits into from
Jun 13, 2023
Merged
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,6 @@ axios(requestOptions);
Media type param, such as <code>raw</code>, <code>diff</code>, or <code>text+json</code>. See <a href="https://developer.github.com/v3/media/">Media Types</a>. Setting <code>options.mediaType.format</code> will amend the <code>headers.accept</code> value.
</td>
</tr>
<tr>
<th>
<code>options.mediaType.previews</code>
</th>
<td>
Array of Strings
</td>
<td>
Name of previews, such as <code>mercy</code>, <code>symmetra</code>, or <code>scarlet-witch</code>. See <a href="https://developer.github.com/v3/previews/">API Previews</a>. If <code>options.mediaType.previews</code> was set as default, the new previews will be merged into the default ones. Setting <code>options.mediaType.previews</code> will amend the <code>headers.accept</code> value. <code>options.mediaType.previews</code> will be merged with an existing array set using <code>.defaults()</code>.
</td>
</tr>
<tr>
<th>
<code>options.data</code>
Expand Down
1 change: 0 additions & 1 deletion src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ export const DEFAULTS: EndpointDefaults = {
},
mediaType: {
format: "",
previews: [] as string[],
},
};
22 changes: 13 additions & 9 deletions src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ export function merge(

const mergedOptions = mergeDeep(defaults || {}, options) as EndpointDefaults;

// mediaType.previews arrays are merged, instead of overwritten
if (defaults && defaults.mediaType.previews.length) {
mergedOptions.mediaType.previews = defaults.mediaType.previews
.filter((preview) => !mergedOptions.mediaType.previews.includes(preview))
.concat(mergedOptions.mediaType.previews);
if (options.url === "/graphql") {
// mediaType.previews arrays are merged, instead of overwritten
if (defaults && defaults.mediaType.previews!.length) {
wolfy1339 marked this conversation as resolved.
Show resolved Hide resolved
mergedOptions.mediaType.previews = defaults.mediaType.previews!
.filter(
(preview) => !mergedOptions.mediaType.previews!.includes(preview)
)
.concat(mergedOptions.mediaType.previews!);
}

mergedOptions.mediaType.previews = mergedOptions.mediaType.previews!.map(
(preview: string) => preview.replace(/-preview/, "")
);
}

mergedOptions.mediaType.previews = mergedOptions.mediaType.previews.map(
(preview: string) => preview.replace(/-preview/, "")
);

return mergedOptions;
}
30 changes: 16 additions & 14 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,29 @@ export function parse(options: EndpointDefaults): RequestOptions {
// e.g. application/vnd.github.v3+json => application/vnd.github.v3.raw
headers.accept = headers.accept
.split(/,/)
.map((preview) =>
preview.replace(
.map((format) =>
format.replace(
/application\/vnd(\.\w+)(\.v3)?(\.\w+)?(\+json)?$/,
`application/vnd$1$2.${options.mediaType.format}`
)
)
.join(",");
}

if (options.mediaType.previews.length) {
const previewsFromAcceptHeader =
headers.accept.match(/[\w-]+(?=-preview)/g) || ([] as string[]);
headers.accept = previewsFromAcceptHeader
.concat(options.mediaType.previews)
.map((preview) => {
const format = options.mediaType.format
? `.${options.mediaType.format}`
: "+json";
return `application/vnd.github.${preview}-preview${format}`;
})
.join(",");
if (url === "/graphql") {
if (options.mediaType.previews!.length) {
const previewsFromAcceptHeader =
headers.accept.match(/[\w-]+(?=-preview)/g) || ([] as string[]);

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of '-'.
headers.accept = previewsFromAcceptHeader
.concat(options.mediaType.previews!)
.map((preview) => {
const format = options.mediaType.format
? `.${options.mediaType.format}`
: "+json";
return `application/vnd.github.${preview}-preview${format}`;
})
.join(",");
}
}
}

Expand Down
45 changes: 0 additions & 45 deletions test/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,51 +89,6 @@ describe("endpoint.defaults()", () => {
});
expect(myEndpoint.DEFAULTS.mediaType).toEqual({
format: "raw",
previews: [],
});
});

it(".defaults() merges mediaType.previews", () => {
const myEndpoint = endpoint.defaults({
mediaType: {
previews: ["foo"],
},
});
const myEndpoint2 = myEndpoint.defaults({
mediaType: {
previews: ["bar"],
},
});

expect(myEndpoint.DEFAULTS.mediaType).toEqual({
format: "",
previews: ["foo"],
});
expect(myEndpoint2.DEFAULTS.mediaType).toEqual({
format: "",
previews: ["foo", "bar"],
});
});

it('.defaults() merges mediaType.previews with "-preview" suffix', () => {
const myEndpoint = endpoint.defaults({
mediaType: {
previews: ["foo-preview"],
},
});
const myEndpoint2 = myEndpoint.defaults({
mediaType: {
previews: ["bar-preview"],
},
});

expect(myEndpoint.DEFAULTS.mediaType).toEqual({
format: "",
previews: ["foo"],
});
expect(myEndpoint2.DEFAULTS.mediaType).toEqual({
format: "",
previews: ["foo", "bar"],
});
});
});
120 changes: 0 additions & 120 deletions test/endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,126 +362,6 @@ describe("endpoint()", () => {
});
});

it("options.mediaType.previews", () => {
const options = endpoint({
method: "GET",
url: "/repos/{owner}/{repo}/issues/{number}",
mediaType: {
previews: ["symmetra"],
},
owner: "octokit",
repo: "endpoint.js",
number: 123,
});

expect(options).toEqual({
method: "GET",
url: "https://api.github.com/repos/octokit/endpoint.js/issues/123",
headers: {
accept: "application/vnd.github.symmetra-preview+json",
"user-agent": userAgent,
},
});
});

it("options.mediaType.previews with -preview suffix", () => {
const options = endpoint({
method: "GET",
url: "/repos/{owner}/{repo}/issues/{number}",
mediaType: {
previews: ["jean-grey-preview", "symmetra-preview"],
},
owner: "octokit",
repo: "endpoint.js",
number: 123,
});

expect(options).toEqual({
method: "GET",
url: "https://api.github.com/repos/octokit/endpoint.js/issues/123",
headers: {
accept:
"application/vnd.github.jean-grey-preview+json,application/vnd.github.symmetra-preview+json",
"user-agent": userAgent,
},
});
});

it("options.mediaType.format + options.mediaType.previews", () => {
const options = endpoint({
method: "GET",
url: "/repos/{owner}/{repo}/issues/{number}",
mediaType: {
format: "raw",
previews: ["symmetra"],
},
owner: "octokit",
repo: "endpoint.js",
number: 123,
});

expect(options).toEqual({
method: "GET",
url: "https://api.github.com/repos/octokit/endpoint.js/issues/123",
headers: {
accept: "application/vnd.github.symmetra-preview.raw",
"user-agent": userAgent,
},
});
});

it("options.mediaType.format + options.mediaType.previews + accept header", () => {
const options = endpoint({
method: "GET",
url: "/repos/{owner}/{repo}/issues/{number}",
headers: {
accept: "application/vnd.foo-preview,application/vnd.bar-preview",
},
mediaType: {
format: "raw",
previews: ["symmetra"],
},
owner: "octokit",
repo: "endpoint.js",
number: 123,
});

expect(options).toEqual({
method: "GET",
url: "https://api.github.com/repos/octokit/endpoint.js/issues/123",
headers: {
accept:
"application/vnd.github.foo-preview.raw,application/vnd.github.bar-preview.raw,application/vnd.github.symmetra-preview.raw",
"user-agent": userAgent,
},
});
});

it("application/octet-stream accept header + previews", () => {
const options = endpoint({
method: "GET",
url: "/repos/{owner}/{repo}/releases/assets/{asset_id}",
headers: {
accept: "application/octet-stream",
},
mediaType: {
previews: ["symmetra"],
},
owner: "octokit",
repo: "endpoint.js",
asset_id: 123,
});

expect(options).toEqual({
method: "GET",
url: "https://api.github.com/repos/octokit/endpoint.js/releases/assets/123",
headers: {
accept: "application/octet-stream",
"user-agent": userAgent,
},
});
});

it("Undefined query parameter", () => {
const options = endpoint({
method: "GET",
Expand Down
3 changes: 0 additions & 3 deletions test/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe("endpoint.merge()", () => {
baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
mediaType: {
format: "",
previews: [],
},
method: "GET",
url: "/orgs/{org}/repos",
Expand Down Expand Up @@ -62,7 +61,6 @@ describe("endpoint.merge()", () => {
baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
mediaType: {
format: "",
previews: [],
},
method: "GET",
url: "/orgs/{org}/repos",
Expand All @@ -81,7 +79,6 @@ describe("endpoint.merge()", () => {
baseUrl: "https://api.github.com",
mediaType: {
format: "",
previews: [],
},
method: "GET",
headers: {
Expand Down
2 changes: 0 additions & 2 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe("endpoint.parse()", () => {
},
mediaType: {
format: "",
previews: [],
},
});
expect(url).toEqual("https://example.com/");
Expand All @@ -45,7 +44,6 @@ describe("endpoint.parse()", () => {
},
mediaType: {
format: "",
previews: ["foo", "bar"],
},
};

Expand Down