-
Notifications
You must be signed in to change notification settings - Fork 2.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
Amplify services provided through withSSRContext have no type definition #7426
Comments
@emilioschepis, for clarity, were you following this setup guide we have for Next.js? Please let us know |
Hi @sammartinez , I was not following that setup guide directly, but the same applies to this section: By adding that code to my component, |
export declare class AmplifyClass {
private _components;
private _config;
private _modules;
Auth: any;
Analytics: any;
API: any;
Credentials: any;
Storage: any;
I18n: any;
Cache: any;
PubSub: any;
Interactions: any;
Pushnotification: any;
UI: any;
XR: any;
Predictions: any;
DataStore: any;
Logger: typeof LoggerClass;
ServiceWorker: any;
register(comp: any): void;
configure(config?: any): {};
addPluggable(pluggable: any): void;
} Looking at the export class AmplifyClass {
// Everything that is `register`ed is tracked here
private _components = [];
private _config = {};
// All modules (with `getModuleName()`) are stored here for dependency injection
private _modules = {};
// for backward compatibility to avoid breaking change
// if someone is using like Amplify.Auth
Auth = null;
Analytics = null;
API = null;
Credentials = null;
Storage = null;
I18n = null;
Cache = null;
PubSub = null;
Interactions = null;
Pushnotification = null;
UI = null;
XR = null;
Predictions = null;
DataStore = null;
Logger = LoggerClass;
ServiceWorker = null;
...
} This is a fairly useless type definition. |
As a current workaround, I am performing this: import { withSSRContext } from "aws-amplify";
import { Auth } from '@aws-amplify/auth';
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const auth = withSSRContext(ctx).Auth as typeof Auth;
let user: any;
try {
user = await auth.currentAuthenticatedUser();
console.log('user is authenticated');
// fetch some data and assign it to the data variable
} catch (err) {
console.log('error: no authenticated user');
}
}; |
That is how I solved the problem as well, it seems like the only option we currently have. |
For good hygiene, might I recommend the following: import { withSSRContext } from "aws-amplify";
- import { Auth } from '@aws-amplify/auth';
+ import type { APIClass } from '@aws-amplify/auth';
export const getServerSideProps: GetServerSideProps = async (ctx) => {
- const auth = withSSRContext(ctx).Auth as typeof Auth;
+ const auth = withSSRContext(ctx).Auth as APIClass;
let user: any;
try {
user = await auth.currentAuthenticatedUser();
console.log('user is authenticated');
// fetch some data and assign it to the data variable
} catch (err) {
console.log('error: no authenticated user');
}
}; |
|
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 don't think this stale, sorry if it is |
We have published an RFC on our plan for improving TypeScript support in Amplify JS & would love to get your feedback & suggestions! |
The developer preview for v6 of Amplify has officially been released with improved support for TypeScript and much more! Please check out our announcement and updated documentation to see what has changed. This issue should be resolved within the dev preview and upcoming General Availability for Amplify v6, but let us know with a comment if there are further issues. |
With the release of the latest major version of Amplify (aws-amplify@>6), this issue should now be resolved! Please refer to our release announcement, migration guide, and documentation for more information. |
Describe the bug
When using
withSSRContext
in a Next.js project, the resulting object is of typeAmplifyClass
.Inside of this object,
Auth
,API
,DataStore
, all have a type ofany
.To Reproduce
Steps to reproduce the behavior:
getServerSideProps
method to the default page.Expected behavior
Auth
and all the other objects have the same types they have in their "client side" version.Code Snippet
The text was updated successfully, but these errors were encountered: