aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

AuthSession interface not exported from function fetchAuthSession #13046

Closed mattiLeBlanc closed 7 months ago

mattiLeBlanc commented 8 months ago

Before opening, please confirm:

JavaScript Framework

Angular

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

None

Environment information

``` # Put output below this line System: OS: macOS 13.5.2 CPU: (10) arm64 Apple M1 Max Memory: 6.09 GB / 64.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.17.1 - /usr/local/bin/node Yarn: 1.22.18 - ~/.npm-global/bin/yarn npm: 8.19.1 - ~/.npm-global/bin/npm pnpm: 8.14.0 - ~/.npm-global/bin/pnpm Browsers: Chrome: 121.0.6167.184 Safari: 16.6 npmPackages: @angular-devkit/build-angular: ^17.0.3 => 17.2.1 @angular/animations: ^17.0.0 => 17.2.2 @angular/cli: ^17.0.3 => 17.2.1 @angular/common: ^17.0.0 => 17.2.2 @angular/compiler: ^17.0.0 => 17.2.2 @angular/compiler-cli: ^17.0.0 => 17.2.2 @angular/core: ^17.0.0 => 17.2.2 @angular/forms: ^17.0.0 => 17.2.2 @angular/platform-browser: ^17.0.0 => 17.2.2 @angular/platform-browser-dynamic: ^17.0.0 => 17.2.2 @angular/router: ^17.0.0 => 17.2.2 @types/jasmine: ~5.1.0 => 5.1.4 aws-amplify: ^6.0.17 => 6.0.17 aws-amplify/adapter-core: undefined () aws-amplify/analytics: undefined () aws-amplify/analytics/kinesis: undefined () aws-amplify/analytics/kinesis-firehose: undefined () aws-amplify/analytics/personalize: undefined () aws-amplify/analytics/pinpoint: undefined () aws-amplify/api: undefined () aws-amplify/api/server: undefined () aws-amplify/auth: undefined () aws-amplify/auth/cognito: undefined () aws-amplify/auth/cognito/server: undefined () aws-amplify/auth/enable-oauth-listener: undefined () aws-amplify/auth/server: undefined () aws-amplify/datastore: undefined () aws-amplify/in-app-messaging: undefined () aws-amplify/in-app-messaging/pinpoint: undefined () aws-amplify/push-notifications: undefined () aws-amplify/push-notifications/pinpoint: undefined () aws-amplify/storage: undefined () aws-amplify/storage/s3: undefined () aws-amplify/storage/s3/server: undefined () aws-amplify/storage/server: undefined () aws-amplify/utils: undefined () jasmine-core: ~5.1.0 => 5.1.2 (4.6.0) karma: ~6.4.0 => 6.4.2 karma-chrome-launcher: ~3.2.0 => 3.2.0 karma-coverage: ~2.2.0 => 2.2.1 karma-coverage-coffee-example: 1.0.0 karma-jasmine: ~5.1.0 => 5.1.0 karma-jasmine-html-reporter: ~2.1.0 => 2.1.0 rxjs: ~7.8.0 => 7.8.1 rxjs/ajax: undefined () rxjs/fetch: undefined () rxjs/operators: undefined () rxjs/testing: undefined () rxjs/webSocket: undefined () tslib: ^2.3.0 => 2.6.2 (1.14.1) typescript: ~5.2.2 => 5.2.2 zone.js: ~0.14.2 => 0.14.4 npmGlobalPackages: @angular/cli: 17.0.3 angular-http-server: 1.10.0 aws-cdk: 2.121.1 aws: 0.0.3-2 firebase-tools: 11.16.1 nativescript: 8.2.3 node-gyp: 8.4.1 npm: 8.19.1 pnpm: 8.14.0 yarn: 1.22.18 ```

Describe the bug

Interface AuthSession is not exported from aws-amplify/auth/ types.

Expected behavior

When using

 getAuthSession(): Observable<AuthSession> {
    return from(fetchAuthSession());
  }

I want to be able to return a typed response of my function getAuthSession. AuthSession is not exported atm because it is part of Promise<import("../Auth/types").AuthSession>;

Reproduction steps

install amplify 6 and create a function in Angular returning the authSession

Code Snippet

// Put your code below this line.

getAuthSession(): Observable { return from(fetchAuthSession()); }

Log output

``` // Put your logs below this line N/A ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

image
mattiLeBlanc commented 8 months ago

Note: VS Code can deduce the output, but it is not strictly typed if I keep the return type empty. ergo:

getAuthSession() {
   return from(fetchAuthSession(());
}
mattiLeBlanc commented 8 months ago

Actually, there are more interfaces not exported, like GraphQLOptions and GraphQLResult. I would lile to use them in my code, like this:

type queryInput = {
  statement: string;
  params: { [key: string]: any };
  type: string;
  iam: boolean;
}

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  private client = generateClient();

  graphql({ statement, params, type, iam = false} : queryInput) {

    const payload: GraphQLOptions = {
      query: statement,
      authMode: iam ? 'iam' : 'userPool',
    }
    if (params) {
      payload.variables = params;
    }
    return from<GraphQLResult>(this.client.graphql(payload));
  }
}

Actually,

image

GraphQLResult seems to be there, but not GraphQLOptions, however it is declared next to GraphQLResult. Is that my VS Code being confused?

ennioVisco commented 8 months ago

I can confirm. As a workaround, for the time being, we defined this in place of AuthSession:

import { fetchAuthSession } from "aws-amplify/auth";
type AmplifyAuthSession = Awaited<ReturnType<typeof fetchAuthSession>>;
nadetastic commented 8 months ago

Hi @mattiLeBlanc thank you for opening this issue. We are currently discussing this internally and will follow up soon.

cwomack commented 7 months ago

Hey, @mattiLeBlanc 👋. Wanted to follow up with you now that this should be implemented. It looks like we introduced this feature in PR #13037 and it's available in v6.0.19 of Amplify. I think you opened this issue just a week or so before the PR was merged 😄.

I'll close this out for now, but feel free to reply back if v6.0.19+ doesn't offer the support you were looking for. Thanks!

mattiLeBlanc commented 7 months ago

@cwomack Thanks for the update and the work! I can't try it yet because I stuck in upgrading to Amplify 6 due to a build issue in Angular in relation to PNPM (https://github.com/aws-amplify/amplify-js/issues/13054). I am looking playing with NX mono repo without pnpm and that is looking promising so hopefully I can use A6 soon.

charlieforward9 commented 5 months ago

We have this need for StorageGetUrlOutput as well.

Has this been resolved in a later version?

James-Prescott commented 1 month ago

Hoping this thread is an acceptable place to raise this issue as it seems closely related.

The following types and interfaces are not exported from aws-amplify/auth. It appears to be causing a TS error when calling fetchAuthSession() inside a function which uses an inferred type for the return. TS struggles to infer the type as it JsonObject (in this case) is not available to it.

Exported variable fooFunc has or is using name 'JsonObject' from external module "[node_modules]/@aws-amplify+core@6.4.4/node_modules/@aws-amplify/core/dist/esm/singleton/Auth/types" but cannot be named.ts(4023)

Is there a reason these aren't exported? I'm happy to raise a separate issue for this or just a PR to export them. Having tried to work around this issue through type coercion and type casting for 2 days, I'm also happy to be told I'm doing something wrong and pointed in a different direction.