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

Incomplete type definition for oauth2.requestToken() return value. #1366

Open
LoganTann opened this issue Oct 2, 2023 · 3 comments
Open
Labels
fixed in v2 fixed in the `2.0` branch

Comments

@LoganTann
Copy link

Context

When working with a developer sandbox, the instanceUrl must be in the form of {yourSandbox}.sandbox.my.salesforce.com instead of {yourSandbox}.sandbox.lightning.force.com (otherwise, any API calls will result in an INVALID_SESSION_ID 401 error).

To avoid this mistake, the /oauth/token endpoint returns the access token, but also the instance_url property.

Issue

The oauth2.requestToken()'s return type is TokenResponse, an interface with the following properties :

export interface TokenResponse {
    access_token: string;
    refresh_token: string;
}

However, it differs to what Salesforce is really returning.

Possible fix

I suggest changing the TokenResponse definition type to this :

export interface TokenResponse {
    access_token: string;
    refresh_token?: string;
    id_token?: string;
    signature: string;
    scope: string;
    instance_url: string;
    id: string;
    token_type: string;
    issued_at: string;
}

It's based on this help article (https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_web_server_flow.htm&type=5), but I couldn't find the real API reference.

Workaround

Create a custom type, and cast the return value to that type.

@cristiand391
Copy link
Member

cristiand391 commented Oct 2, 2023

Hi, I think you are referring to the types in @types/jsforce:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/2cfe1ce383dd04d9f96ccc2febe53ca58525e7ef/types/jsforce/oauth2.d.ts#L17

these are community maintained so you can probably open a PR there but we don't maintain it.

jsforce v2 (written in TS) has the suggested TokenResponse type:

jsforce/src/oauth2.ts

Lines 48 to 58 in b7ac34f

export type TokenResponse = {
token_type: 'Bearer';
id: string;
access_token: string;
refresh_token?: string;
signature: string;
issued_at: string;
instance_url: string;
sfdc_community_url?: string;
sfdc_community_id?: string;
};

@cristiand391 cristiand391 added the fixed in v2 fixed in the `2.0` branch label Oct 2, 2023
@LoganTann
Copy link
Author

Ooh, I see.

@cristiand391 Should I keep this issue open then ?

@cristiand391
Copy link
Member

let's leave it open in case anyone wants to fix it in @types/jsforce, I'll close in once jsforce v3 is GA since you'll no longer need @types/jsforce 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in v2 fixed in the `2.0` branch
Projects
None yet
Development

No branches or pull requests

2 participants