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

πŸ“… 6/18 @ 12:30pm PT - Advanced TypeScript for the future Octokit SDK with @orta #29

Closed
5 of 7 tasks
gr2m opened this issue Jun 14, 2021 · 2 comments
Closed
5 of 7 tasks
Labels
show Preparation issue for a live show

Comments

@gr2m
Copy link
Owner

gr2m commented Jun 14, 2021

πŸ’πŸ» Advanced TypeScript for the future Octokit SDK
πŸ“… Friday, June 18, 2021
πŸ• 12:30pm Pacific Time (in your timezone)
πŸŽ™οΈ with @orta
πŸ“ https://twitch.tv/gregorcodes
🏷️ TypeScript, Octokit


Extending constructor options by plugins.

The Octokit SDK is built using a modular plugin architecture. The core package is @octokit/core, which can be extended using plugins, e.g. like this

// plugin.js
module.exports = function helloWorldPlugin(octokit, options = { greeting: "Hello" }) => {
  // add a custom method
  return {
    helloWorld: () => console.log(`${options.greeting}, world!`);
  }
};

// index.js
const { Octokit } = require("@octokit/core")
const helloWorldPlugin = require("./plugin")

const MyOctokit = Octokit.plugin(helloWorldPlugin);

const octokit = new MyOctokit({ greeting: "Moin moin" });
octokit.helloWorld(); // logs "Moin moin, world!"

The important bit is that typing octokit. will suggest both Octokit's core methods such as octokit.request and octokit.graphql, but also octokit.helloWorld().

But something that is not currently possible is to extend the types the types for Octokit's constructor options so that when I type new Octokit({ it would suggest { greeting } as an option.

I started a pull request at gr2m/javascript-plugin-architecture-with-typescript-definitions#56 to implement it with Orta.

Inherit constructor options

Currently Octokit has no required options. But one thing we want to improve is the developer experience to build code for GitHub Enterprise Server or GitHub AE. In order to differentiate between the two we will probably add some kind of version parameter that can be set to api.github.com, ghes3.2 or ghes-3.1-compatible.

The other reason that we will require constructor options inheritance will be types for the Authentication strategies (see next section).

So given that version will be a required parameter, this code should be possible without any type errors

const OctokitGHE31 = Octokit.defaults({ version: 'ghe-3.1' })
const octokit = new OctokitGHE31()

Types for Authentication strategies

I have a long standing open issue for this one: octokit/core.js#323

Basically what I want a better developer experience when using authentication strategies such as @octokit/auth-app

import { Octokit } from "@octokit/core";
import { createAppAuth } from "@octokit/auth-app"
const octokit = new Octokit({
  authStrategy: createAppAuth,
  auth: {
    // should provide IntelliSense for createAppAuth options:
    // https://github.com/octokit/auth-app.js/#createappauthoptions-or-new-octokit-auth-
  }
})

This should work together with the inheritence of options types from Octokit.defaults() so this will be possible

import { Octokit } from "@octokit/core";
import { createAppAuth } from "@octokit/auth-app"

const AppOctokit = Octokit.defaults({
  authStrategy: createAppAuth
})

const octokit = new AppOctokit({
  auth: {
    // should provide IntelliSense for createAppAuth options:
    // https://github.com/octokit/auth-app.js/#createappauthoptions-or-new-octokit-auth-
  }
})

Outline

  1. The TypeScript for today's @octokit and where it falls short
  2. Make Types extendable by plugins (feat: extend Base constructor options with pluginsΒ javascript-plugin-architecture-with-typescript-definitions#56)
  3. Remember default options set with Octokit.defaults()

Preparation

Recording

will be added after the show

Shownotes

@gr2m gr2m added the show Preparation issue for a live show label Jun 14, 2021
@gr2m
Copy link
Owner Author

gr2m commented Jun 18, 2021

Video is now available on Twitch:
https://www.twitch.tv/videos/1060393045

@oscard0m
Copy link

Linking these tweets following up the show:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
show Preparation issue for a live show
Projects
None yet
Development

No branches or pull requests

2 participants