Skip to content

Commit 41ddc9e

Browse files
authored
feat: add support for Octokit authentication strategies (#100)
1 parent 1d0f5db commit 41ddc9e

File tree

6 files changed

+458
-78
lines changed

6 files changed

+458
-78
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Create new Octokit JS project
22

3-
> "[npm init](https://docs.npmjs.com/cli/v7/commands/npm-init)" script to create a new folder and repository for an Octokit JavaScript project
3+
> "[npm init](https://docs.npmjs.com/cli/v7/commands/npm-init)" script to create a new folder and repository for an Octokit JavaScript module (plugin, authentication strategy, or otherwise)
44
55
## Usage
66

create-octokit-project.js

Lines changed: 120 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ async function main() {
173173

174174
await createPackageJson(answers);
175175
console.log("Install dependencies");
176-
const dependencies = [
176+
const dependencies = [];
177+
const devDependencies = [
177178
"@octokit/tsconfig",
178179
"@pika/pack",
179180
"@pika/plugin-ts-standard-pkg",
@@ -188,15 +189,22 @@ async function main() {
188189
];
189190

190191
if (answers.supportsBrowsers) {
191-
dependencies.push("@pika/plugin-build-web");
192+
devDependencies.push("@pika/plugin-build-web");
192193
}
193194
if (answers.supportsNode) {
194-
dependencies.push("@pika/plugin-build-node");
195+
devDependencies.push("@pika/plugin-build-node");
195196
}
196-
if (answers.isPlugin) {
197-
dependencies.push("@octokit/core");
197+
if (answers.isPlugin || answers.isAuthenticationStrategy) {
198+
devDependencies.push("@octokit/core");
199+
}
200+
if (answers.isAuthenticationStrategy) {
201+
dependencies.push("@octokit/types");
202+
}
203+
await command(`npm install --save-dev ${devDependencies.join(" ")}`);
204+
205+
if (dependencies.length) {
206+
await command(`npm install ${dependencies.join(" ")}`);
198207
}
199-
await command(`npm install --save-dev ${dependencies.join(" ")}`);
200208

201209
await command(`git add package.json`);
202210
await command(`git commit -m 'build(package): initial version'`);
@@ -240,41 +248,42 @@ async function main() {
240248
241249
import { ${answers.exportName} } from "../src";
242250
243-
describe("Smoke test", () => {
244-
it("{ ${answers.exportName} } export is a function", () => {
245-
expect(${answers.exportName}).toBeInstanceOf(Function);
246-
});
247-
248-
it("${answers.exportName}.VERSION is set", () => {
249-
expect(${answers.exportName}.VERSION).toEqual("0.0.0-development");
250-
});
251-
252-
it("Loads plugin", () => {
253-
expect(() => {
254-
const TestOctokit = Octokit.plugin(${answers.exportName})
255-
new TestOctokit();
256-
}).not.toThrow();
257-
});
251+
describe("Smoke test", () => {
252+
it("{ ${answers.exportName} } export is a function", () => {
253+
expect(${answers.exportName}).toBeInstanceOf(Function);
254+
});
255+
256+
it("${answers.exportName}.VERSION is set", () => {
257+
expect(${answers.exportName}.VERSION).toEqual("0.0.0-development");
258+
});
259+
260+
it("Loads plugin", () => {
261+
expect(() => {
262+
const TestOctokit = Octokit.plugin(${answers.exportName})
263+
new TestOctokit();
264+
}).not.toThrow();
258265
});
266+
});
259267
`
260268
);
261-
}
262-
await writePrettyFile(
263-
"test/smoke.test.ts",
264-
`
265-
import { ${answers.exportName} } from "../src";
269+
} else {
270+
await writePrettyFile(
271+
"test/smoke.test.ts",
272+
`
273+
import { ${answers.exportName} } from "../src";
266274
267-
describe("Smoke test", () => {
268-
it("is a function", () => {
269-
expect(${answers.exportName}).toBeInstanceOf(Function);
270-
});
275+
describe("Smoke test", () => {
276+
it("is a function", () => {
277+
expect(${answers.exportName}).toBeInstanceOf(Function);
278+
});
271279
272-
it("${answers.exportName}.VERSION is set", () => {
273-
expect(${answers.exportName}.VERSION).toEqual("0.0.0-development");
280+
it("${answers.exportName}.VERSION is set", () => {
281+
expect(${answers.exportName}.VERSION).toEqual("0.0.0-development");
282+
});
274283
});
275-
});
276-
`
277-
);
284+
`
285+
);
286+
}
278287

279288
await command(`git add test`);
280289
await command(`git commit -m 'test: initial version'`);
@@ -289,12 +298,10 @@ async function main() {
289298
await writePrettyFile(
290299
"src/index.ts",
291300
`
301+
import { Octokit } from "@octokit/core";
292302
import { VERSION } from "./version";
293303
294-
type Octokit = any;
295-
type Options = {
296-
[option: string]: any;
297-
};
304+
type Options = Record<string, unknown>;
298305
299306
/**
300307
* @param octokit Octokit instance
@@ -304,6 +311,78 @@ async function main() {
304311
${answers.exportName}.VERSION = VERSION;
305312
`
306313
);
314+
} else if (answers.isAuthenticationStrategy) {
315+
await writePrettyFile(
316+
"src/index.ts",
317+
`
318+
import { VERSION } from "./version";
319+
import { auth } from "./auth";
320+
import { hook } from "./hook";
321+
import { StrategyOptions, AuthOptions, Authentication } from "./types";
322+
323+
export type Types = {
324+
StrategyOptions: any;
325+
AuthOptions: any;
326+
Authentication: any;
327+
};
328+
329+
export const ${answers.exportName}: StrategyInterface = function ${answers.exportName}(
330+
options: StrategyOption
331+
) {
332+
return Object.assign(auth.bind(null, options), {
333+
hook: hook.bind(null, options),
334+
});
335+
};
336+
337+
`
338+
);
339+
await writePrettyFile(
340+
"src/types.ts",
341+
`
342+
export type Types = {
343+
StrategyOptions: any;
344+
AuthOptions: any;
345+
Authentication: any;
346+
};
347+
`
348+
);
349+
await writePrettyFile(
350+
"src/auth.ts",
351+
`
352+
import { AuthOptions, Authentication } from "./types";
353+
354+
export async function auth(options: AuthOptions): Promise<Authentication> {
355+
// TODO: add implementation
356+
}
357+
`
358+
);
359+
await writePrettyFile(
360+
"src/hook.ts",
361+
`
362+
import {
363+
EndpointDefaults,
364+
EndpointOptions,
365+
OctokitResponse,
366+
RequestInterface,
367+
RequestParameters,
368+
Route,
369+
} from "@octokit/types";
370+
import { AuthOptions } from "./types";
371+
372+
type AnyResponse = OctokitResponse<any>
373+
374+
export async function hook(
375+
options: AuthOptions,
376+
request: RequestInterface,
377+
route: Route | EndpointOptions,
378+
parameters: RequestParameters = {}
379+
): Promise<AnyResponse> {
380+
// TODO: add implementation
381+
// probably something like setting the authorization header
382+
return request(route, parameters);
383+
}
384+
`
385+
);
307386
} else {
308387
const isClass = /^[A-Z]/.test(answers.exportName);
309388

@@ -351,6 +430,8 @@ async function main() {
351430
packageName: answers.packageName,
352431
repository: answers.repository,
353432
isPlugin: answers.isPlugin,
433+
isAuthenticationStrategy: answers.isAuthenticationStrategy,
434+
octokitUsageExample: answers.octokitUsageExample,
354435
exportName: answers.exportName,
355436
supportsBrowsers: answers.supportsBrowsers,
356437
supportsNode: answers.supportsNode,

lib/create-issue-templates.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ async function createIssueTemplates(isOctokitOrg) {
5454
await writePrettyFile(
5555
".github/ISSUE_TEMPLATE/config.yml",
5656
`blank_issues_enabled: false
57-
contact_links:
58-
- name: 🆘 I need Help
59-
url: https://github.com/octokit/rest.js/discussions
60-
about: Got a question? An idea? Feedback? Start here.`
57+
contact_links:
58+
- name: 🆘 I need Help
59+
url: https://github.com/octokit/rest.js/discussions
60+
about: Got a question? An idea? Feedback? Start here.`
6161
);
6262
return;
6363
}

0 commit comments

Comments
 (0)