-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add Types to Responses #732
Conversation
in favor of handlebars
since the value is always a `{meta, data}` object when successful
54e860a
to
6e15011
Compare
to the TypeScript definition file
Hey Phil, thanks for all the extensive pull request, love to see work being merged with octokat :) I'm heads down in finishing up other work right now but let me know if you have any blockers. One question that I have is: how will we keep |
Thanks for responding! I originally generated I think that something similar could be done to at least validate that the Possible Blocker: I pushed up an example that generates these tests but since octokit might need fixtures to run, I am not sure where those fixtures should be referenced (maybe in If you run // Frontmatter. See scripts/templates/generate-response-types-tests.tpl for the frontmatter
it('Responds with a "RepoEvent" when calling octokit.activity.getEvents(...)', async () => {
const expectedEmpty = false
const expectedArray = true
const expectedType = "RepoEvent"
const params = reifyParams({"page":{"type":"number","description":"Page number of the results to fetch."},"per_page":{"type":"number","default":"30","description":"A custom page size up to 100. Default is 30."}})
const octokit = newGitHub()
const {data} = await octokit.activity.getEvents(params)
validateType(`(${expectedType})`, data, expectedType, expectedArray)
}) |
based on information in lib/routes.json
8e5cd85
to
e54ff9f
Compare
sounds good! I'll be looking into auto generated & updating the routes.json file soon, I think that'll help |
@philschatz Heads up that I'm getting this now when I try to compile a typescript project using the newest version: src/robot.ts(149,53): error TS2345: Argument of type '{ debug: boolean; host: string; pathPrefix: string; logger: LoggerWithTarget; }' is not assignable to parameter of type 'Options'.
Property 'agent' is missing in type '{ debug: boolean; host: string; pathPrefix: string; logger: LoggerWithTarget; }'. Writing the option as |
Hey follow up on my last comment, the script to generate something like routes.json is now happening at octokit/routes#1. Hope that will be helpful to generate much more useful Typescript definitions, too. |
@tcbyrd Thank you for catching that! |
I think we need to change When I use
But if I use
|
This Pull Request now uses a JSON Schema to describe the Response Types, and @JasonEtco's suggestion of using json-schema-to-typescript to convert the schema into TypeScript definitions (per discussion in octokit/routes#20 ). You can paste the schema file into https://transform.now.sh/json-schema-to-ts/ to see the TypeScript. Validating the schema might be automatically possible by using the existing tests and adding a It seems like octokit/routes might be a better place for the Schema to live (since it is language agnostic) but I included it here because:
|
I started a PR to use I’m not yet sure about how we achieve the same with |
|
I would prefer not to use different method names in order to stay as close to the REST API as possible.
Sounds like a plan! |
#822 is merged, happy to move forward with this now. #822 introduced aliasing of method names and parameters, does Typescript support that? I want to stay close to the API documentation so some parameters like |
oh also if you could help me double check the new version, especially if you use typescript, I'd appreciate it: #844 |
@@ -8,9 +8,9 @@ declare namespace Github { | |||
type json = any | |||
type date = string | |||
|
|||
export interface AnyResponse { | |||
export interface AnyResponse<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense now to rename it to Response<T>
? An alias AnyResponse = Response<any>
could be kept too if needed for compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also do Response<T = any>
Hey friends, would you still like to finish this up? Anything I can help with? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I’m sorry, I’m going to close this. I don’t want to add the |
This adds type information to responses (useful for probot/probot#372 and fixes #563). Currently they are of the form
Github.AnyResponse
.This changes them to be of the form (depending on the method that is called):
Github.AnyResponse<void>
: a method that removes an objectGithub.AnyResponse<Github.Response.Gist>
: a method that yields a single objectGithub.AnyResponse<Array<Github.Response.RepoEvent>>>
: a method that yields multiple objectsIt also creates a the response types like this:
This work was originally done in philschatz/octokat.js#176
Screenshots
Responses now have type information:
Includes the method documentation in the
index.d.ts
file so it shows up in TypeScript editors:TODO
notyield anAnyResponse
typeResponse.Boolean
(which is an''
when true and an err when false),AnyResponse<Array<...>>
onesPossible TODO
Array<...>
when the JSON containsper_page
as one of the parametersDELETE
methods yield avoid
get: MethodWithParams<Github.PullRequestGetParams, Github.Response.RepoPullRequest>
rather thanget(params: Github.PullRequestsGetParams, callback?: Github.Callback<Github.Response.RepoPullRequest>): Promise<Github.AnyResponse<Github.Response.RepoPullRequest>>;