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

Add type definitions via index.d.ts #2514

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions packages/ember-simple-auth/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
interface Callback {
(): void;
}
type Data = Record<string, unknown>;
interface SessionData<T> {
authenticated: T;
}

declare module 'ember-simple-auth/services/session' {
import Evented from '@ember/object/evented';
Copy link

Choose a reason for hiding this comment

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

All of the imports could be import type, i guess:

-import Evented from '@ember/object/evented';
+import type Evented from '@ember/object/evented';
-import Service from '@ember/service';
+import type Service from '@ember/service';
-import EmberObject from '@ember/object';
+import type EmberObject from '@ember/object';

-import BaseSessionStore from 'ember-simple-auth/session-stores/base';
+import type BaseSessionStore from 'ember-simple-auth/session-stores/base';
-import CookieSessionStore from 'ember-simple-auth/session-stores/base';
+import type CookieSessionStore from 'ember-simple-auth/session-stores/base';
-import BaseAuthenticator from 'ember-simple-auth/authenticators/base';
+import type BaseAuthenticator from 'ember-simple-auth/authenticators/base';
-import SimpleAuthSessionService from 'ember-simple-auth/services/session';
+import type SimpleAuthSessionService from 'ember-simple-auth/services/session';

import Service from '@ember/service';
import type BaseSessionStore from 'ember-simple-auth/session-stores/base';
import type Transition from '@ember/routing/-private/transition';
Copy link

@johanrd johanrd Jan 19, 2024

Choose a reason for hiding this comment

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

this could now be imported from the public transition type:

-import type Transition from '@ember/routing/-private/transition';
+import type Transition from '@ember/routing/transition'


export default class SessionService<T> extends Service.extend(Evented) {
isAuthenticated: boolean;
data: SessionData<T>;
store: BaseSessionStore;
attemptedTransition: Transition | null;

authenticate(authenticator: string, ...args: unknown[]): Promise<void>;
invalidate(...args: unknown[]): Promise<unknown>;
requireAuthentication(transition: Transition, routeOrCallback: string | Callback): boolean;
prohibitAuthentication(routeOrCallback: string | Callback): boolean;
handleAuthentication(routeAfterAuthentication: string): void;
handleInvalidation(routeAfterInvalidation: string): void;
setup(): Promise<void>;
}
}

declare module 'ember-simple-auth/authenticators/base' {
import EmberObject from '@ember/object';
import Evented from '@ember/object/evented';

export default class extends EmberObject.extend(Evented) {
restore(data: Data): Promise<unknown>;
authenticate(...args: unknown[]): Promise<unknown>;
Comment on lines +36 to +37
Copy link

Choose a reason for hiding this comment

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

I believe Promise<unknown> here can actually be Promise<SessionData<T>>.
I don't know how to make sure SessionService<T>.data and these return values are in synced though.

invalidate(data: Data, ...args: unknown[]): Promise<unknown>;
}
}

declare module 'ember-simple-auth/session-stores/base' {
import EmberObject from '@ember/object';
import Evented from '@ember/object/evented';

export default class extends EmberObject.extend(Evented) {
persist(data: SessionData<Data>): Promise<unknown>;
restore(): Promise<SessionData<Data>>;
clear(): Promise<void>;
}
}

declare module 'ember-simple-auth/session-stores/local-storage' {
import BaseSessionStore from 'ember-simple-auth/session-stores/base';

export default class extends BaseSessionStore {
key: string;
}
}

declare module 'ember-simple-auth/session-stores/cookie' {
import BaseSessionStore from 'ember-simple-auth/session-stores/base';

export default class extends BaseSessionStore {
cookieDomain: string;
cookieExpirationTime: null | number;
cookieName: string;
cookiePath: string;
sameSite: null | 'Strict' | 'Lax';
}
}

declare module 'ember-simple-auth/session-stores/adaptive' {
import CookieSessionStore from 'ember-simple-auth/session-stores/base';

export default class extends CookieSessionStore {
localStorageKey: string;
}
}

declare module 'ember-simple-auth/session-stores/ephemeral' {
import BaseSessionStore from 'ember-simple-auth/session-stores/base';

export default class extends BaseSessionStore { }
}

declare module 'ember-simple-auth/session-stores/session-storage' {
import BaseSessionStore from 'ember-simple-auth/session-stores/base';

export default class extends BaseSessionStore {
key: string;
}
}

declare module 'ember-simple-auth/authenticators/test' {
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';

export default class extends BaseAuthenticator { }
}

declare module 'ember-simple-auth/authenticators/devise' {
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';

export default class extends BaseAuthenticator {
serverTokenEndpoint: string;
resourceName: string;
tokenAttributeName: string;
identificationAttributeName: string;
}
}

declare module 'ember-simple-auth/authenticators/oauth2-implicit-grant' {
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';

export default class extends BaseAuthenticator { }
}

declare module 'ember-simple-auth/authenticators/oauth2-password-grant' {
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';

export default class extends BaseAuthenticator {
clientId: string | null;
serverTokenEndpoint: string;
serverTokenRevocationEndpoint: string | null;
refreshAccessTokens: boolean;
}
}

declare module 'ember-simple-auth/authenticators/torii' {
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';

export default class extends BaseAuthenticator { }
}

declare module 'ember-simple-auth/test-support' {
import SimpleAuthSessionService from 'ember-simple-auth/services/session';
export function currentSession(): SimpleAuthSessionService<Data>;
export function authenticateSession(sessionData: Data): Promise<void>;
export function invalidateSession(): Promise<void>;
}
2 changes: 2 additions & 0 deletions packages/ember-simple-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"doc": "doc",
"test": "tests"
},
"types": "./index.d.ts",
"scripts": {
"build": "ember build",
"lint": "npm-run-all --aggregate-output --continue-on-error --parallel \"lint:!(fix)\"",
Expand All @@ -36,6 +37,7 @@
"@embroider/test-setup": "^2.0.0",
"@glimmer/component": "^1.0.0",
"@glimmer/tracking": "^1.0.0",
"@types/ember": "4.0.3",
"babel-eslint": "^10.1.0",
"broccoli-asset-rev": "^3.0.0",
"ember-auto-import": "^2.4.2",
Expand Down