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

Support generics in generated types #171

Closed
heuels opened this issue Jul 30, 2020 · 4 comments
Closed

Support generics in generated types #171

heuels opened this issue Jul 30, 2020 · 4 comments

Comments

@heuels
Copy link
Contributor

heuels commented Jul 30, 2020

Hey guys 🙂

First off, thanks for the awesome project! After #168 got merged I started using generated types for workflow_dispatch event and it works great. However having to type cast inputs of the event through any pains me a lot:

type WebhookPayloadWorkflowDispatchInputs = {};
type WebhookPayloadWorkflowDispatch = {
  inputs: WebhookPayloadWorkflowDispatchInputs;
  ref: string;
  repository: PayloadRepository;
  organization: WebhookPayloadWorkflowDispatchOrganization;
  sender: WebhookPayloadWorkflowDispatchSender;
  workflow: string;
};
type FooBarInput = { foo: string; bar: string };

const onWorkflowDispatch = (event: Webhooks.WebhookPayloadWorkflowDispatch) => {
  const { foo, bar } = (event.inputs as any) as FooBarInput;
  ...
}

However, I do believe that a better developer experience can be achieved by introducing generics in generated types:

type WebhookPayloadWorkflowDispatch<T> = {
  inputs: T;
  ref: string;
  repository: PayloadRepository;
  organization: WebhookPayloadWorkflowDispatchOrganization;
  sender: WebhookPayloadWorkflowDispatchSender;
  workflow: string;
};
type FooBarInput = { foo: string; bar: string };

const onWorkflowDispatch = (event: Webhooks.WebhookPayloadWorkflowDispatch<FooBarInput>) => {
  const { foo, bar } = event.inputs;
  ...
}

It is something that should be raised here or in gimenete/type-writer? I'd be happy to move this issue there if that's the case. Anyway, I would be happy to assist with a PR if/when we figure out some kind of a solution for this.

@gr2m
Copy link
Contributor

gr2m commented Jul 30, 2020

I think maybe we could start defining JSON Schemas in https://github.com/octokit/webhooks, besides the examples? We could still use the examples to validate against the schema, but use the schema to generate the types for @octokit/webhooks.

There is an interest in JSON Schemas for webhooks from both users and GitHub, see github/rest-api-description#21. We could accelerate that process with a community run effort in octokit/webhooks, similar how https://github.com/octokit/routes/ was the starting point for https://github.com/github/rest-api-description

What do you think?

@heuels
Copy link
Contributor Author

heuels commented Jul 31, 2020

Using JSON Schemas does seem reasonable to me. However I just looked through the "Webhook events and payloads" and it looks like that descriptions there are too inconsistent for us to be able to parse them. I guess we could start by composing the schema manually.

@gr2m
Copy link
Contributor

gr2m commented Jul 31, 2020

I guess we could start by composing the schema manually.

Yes, that's what I would suggest. We can bootstrap them the examples in the repository, see https://github.com/octokit/webhooks/tree/master/payload-examples/api.github.com. Unfortunately @gimenete/type-writer does not support a JSON Schema as far as I can tell, but if I recall correctly, I used another library to create schemas from JSON inputs. And more examples can be added easily which will help make the schema more accurate and will help with testing the schema in future.

Shall we take this discussion over to the octokit/webhooks repository?

@gr2m
Copy link
Contributor

gr2m commented Jul 31, 2020

closing in favor of octokit/webhooks#136

@gr2m gr2m closed this as completed Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants