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

Use @types/simple-oauth2 instead of redoing it #225

Closed

Conversation

voxpelli
Copy link
Contributor

@voxpelli voxpelli commented Aug 25, 2023

Prior to #172 the Credentials was typed to a much more slim subset of what simple-oauth2 currently accepts, but in #172 (which contained #170 / #152) it was extended to pretty much be the full typing of simple-oauth2, but without actually referring to @types/simple-oauth2.

This PR adds @types/simple-oauth2 and uses the types from that one instead – and does so in a backwards compatible way by keeping ProviderConfiguration and Credentials.

My personal motivation for this was that I wanted to add a HTTP Header to some requests (I want to add a User-Agent as I personally think that's good practice to do) and when I looked around if it was supported I realized that it was, but through the Credentials['http'] that's currently typed as:

http?: {} | undefined;

This is what it looks like in @types/simple-oauth2, much more complete:

http?: Omit<WreckHttpOptions, 'baseUrl' | 'headers' | 'redirects' | 'json'> & {
    baseUrl?: undefined;
    headers?: {
        /**
         * Acceptable http response content type. Defaults to application/json
         */
        accept?: string;
        /**
         * Always overriden by the library to properly send the required credentials on each scenario
         */
        authorization?: string;
        [key: string]: unknown
    } | undefined;
    /**
     * Number or redirects to follow. Defaults to false (no redirects)
     */
    redirects?: false | number | undefined;
    /**
     * JSON response parsing mode. Defaults to strict
     */
    json?: boolean | "strict" | "force" | undefined;
 } | undefined;

When looking through the git log, PR:s and issues I can't find any mentioning of why these types wasn't referred to instead of duplicating them. Maybe the @types/simple-oauth2 wasn't available at the time, but now that they are I think its a good idea to refer to them.

Checklist

voxpelli added a commit to voxpelli/fastify-oauth2 that referenced this pull request Aug 25, 2023
Every library appends their user-agent to the one that's been set at a level above them.

Hence any provided user-agent comes before the default user agent.

To provide context, the version number of `@fastify/oauth2` + the home page of it is included in the User-Agent. That way an API can diagnose possibly faulty consumers and file an issue.

If someone wouldn't want to expose this, they can always set a custom User-Agent header directly in the http settings. Especially if fastify#225 gets merged.
Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets discuss this first:

By putting it as dependency and not dev dependency the types package will also be installed as production dependencies by Projects that use this plugin.

@voxpelli
Copy link
Contributor Author

By putting it as dependency and not dev dependency the types package will also be installed as production dependencies by Projects that use this plugin.

Yes, that is unfortunate, it's a balancing act between smaller install size and correct/complete types I think.

One improvement could be that this module stops blankety passing through the options to simple-oauth2 and instead manages its own.

Another could be to add these types as a dev dependency and validate in the type tests that the types defined in this module are indeed assignable to the one in simple-oauth2

The latter I would then like to complement with fleshing out some of the http options (which in the simple-oauth2 types extend from wreck, so would need to be partially copied from there as well)

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 26, 2023

Maybe better to talk to the simple-oauth2 team to merge this PR
lelylan/simple-oauth2#436

@voxpelli
Copy link
Contributor Author

@Uzlopak One doesn't exclude the other. Also, that PR has some of the same concerns (lelylan/simple-oauth2#436 (comment)) as I raise here.

@voxpelli
Copy link
Contributor Author

I'll make an alternative PR soon that avoids adding this as a prod dependency

voxpelli added a commit to voxpelli/fastify-oauth2 that referenced this pull request Aug 26, 2023
This is an alternative to fastify#225 where rather than using `@types/simple-oauth2` in the published types we ensure at test time that the local types are indeed compatible with the published types of `@types/simple-oauth2`.

That way a consumer that wants to have access to the full types of `simple-oauth2` can add `@types/simple-oauth2` in their own project, knowing that it will be compatible
@voxpelli
Copy link
Contributor Author

voxpelli commented Aug 26, 2023

@Uzlopak Perhaps the solution in #229 is better then?

voxpelli added a commit to voxpelli/fastify-oauth2 that referenced this pull request Aug 26, 2023
Every library appends their user-agent to the one that's been set at a level above them.

Hence any provided user-agent comes before the default user agent.

To provide context, the version number of `@fastify/oauth2` + the home page of it is included in the User-Agent. That way an API can diagnose possibly faulty consumers and file an issue.

If someone wouldn't want to expose this, they can always set a custom User-Agent header directly in the http settings. Especially if fastify#225 gets merged.
@voxpelli
Copy link
Contributor Author

Closing in favor of #229

@voxpelli voxpelli closed this Aug 27, 2023
@voxpelli voxpelli deleted the voxpelli/use-simple-oauth2-types branch August 27, 2023 15:27
Uzlopak pushed a commit that referenced this pull request Aug 27, 2023
* Fix `test:coverage` command

* Add default user-agent + enable custom one

Every library appends their user-agent to the one that's been set at a level above them.

Hence any provided user-agent comes before the default user agent.

To provide context, the version number of `@fastify/oauth2` + the home page of it is included in the User-Agent. That way an API can diagnose possibly faulty consumers and file an issue.

If someone wouldn't want to expose this, they can always set a custom User-Agent header directly in the http settings. Especially if #225 gets merged.

* Update index.js

Co-authored-by: Manuel Spigolon <[email protected]>

* Fix feedback, allow disabling of user-agent

* Simplify user-agent string

* Move `credentials` initialization to the top

* Naming tweak in test

* Remove multi-`user-agent` use

The RFC 9110 states:

"The User-Agent field value consists of one or more product identifiers, each followed by zero or more comments (Section 5.6.5), which together identify the user agent software and its significant subproducts. By convention, the product identifiers are listed in decreasing order of their significance for identifying the user agent software."

But since many are not aware of it, it may be better to remove it

---------

Co-authored-by: Manuel Spigolon <[email protected]>
mcollina pushed a commit that referenced this pull request Sep 4, 2023
This is an alternative to #225 where rather than using `@types/simple-oauth2` in the published types we ensure at test time that the local types are indeed compatible with the published types of `@types/simple-oauth2`.

That way a consumer that wants to have access to the full types of `simple-oauth2` can add `@types/simple-oauth2` in their own project, knowing that it will be compatible
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

Successfully merging this pull request may close these issues.

2 participants