0.1.19 • Published 6 years ago
@acknow-srl/user v0.1.19
User
Handles users and their metadata on a GraphQL server.
AckUserModule (Module)
Methods
- forRoot(config: AckUserConfig): void: configure the connection to the GraphQL server.
AckUserConfig (Interface)
- server (string): GraphQL server URL.
AckUserListOptions (Interface)
- attrs (string[]): An array of user's attributes to read when listing all users.
- orderBy (string): The attribute to sort the user list by. You can prepend a "-" sign to sort users in descending order.
User (Interface)
A user model. It must always have an id attribute. All other attributes are optional.
AckAvatarConfig (Interface)
Describes avatars display options.
- rating (string): either
g,pg,rorx. Please, refer to Gravatar documentation. If not provided or empty, defaults to an empty string (equivalent tog). - secure (boolean|null):
trueto force HTTP with SSL (HTTPS),falseto force HTTP,nullto automatically use the current protocol. Defaults tonull. - defaultAvatar (string): default avatar type. It can be the URL to a custom stored image or a predefined Gravatar type (
gravatar,gravatar_default,mm,mystery,mysteryman,mp,mystery-person,identicon,monsterid,wavatar,retro,robohash,blank,404). Please, refer to Gravatar documentation. Defaults to an empty string (equivalent togravatar). - forceDefault (boolean):
trueto always force the default avatar,falseto use the real user's avatar (if exists). Defaults tofalse.
App (Interface)
- userId (number): The user ID.
- appId (string): The app ID.
- data (any|null): JSON object with the app data or null (if there are no data).
AckUser (Service)
It is provided in root, so it is available to the whole app.
Methods
- me(): Observable<User|null>: returns an
Observablewith the authenticated user object, if exists. Otherwise, returns anObservablewithnull. - read(options: AckUserListOptions): Observable<User[]>: returns an
Observablewith all users, based on the given options (see AckUserListOptions Interface). Otherwise, returns anObservablewith an empty array. - getById(userId: number): Observable<User|null>: returns an
Observablewith user data based on the given user ID. If the user does not exists, returns anObservablewithnull. - avatar(hashOrUser: User|string, size: number, config: AckAvatarConfig|null): return the URL to the avatar for a given user or MD5 hash, based on a given size and other options (see the
AckAvatarConfiginterface).
AckApp (Service)
It is provided in root, so it is available to the whole app.
Methods
- read(userId: number): Observable<App[]>: returns an
Observablewith all app data for the given user ID. Otherwise, returns returns anObservablewith an empty array. - get(app: App): Observable<App|null>: Returns an
Observablewith app data based on user ID and app ID. Otherwise, returns anObservablewithnull. - create(app: App): Observable<App|null>: Create an app for a given user. Returns an
Observablewith the created app. Otherwise, returns returns anObservablewithnull. - update(app: App): Observable<App|null>: Updates app data based on a given user ID and app ID. Returns an
Observablewith the updated app. Otherwise, returns returns anObservablewithnull. - delete(app: App): Observable<string>: Delete an app based on a given user ID and app ID. Returns an
Observablewith the deleted internal Postgraphile ID. Otherwise, returns returns anObservablewith an empty string.
Example
AckAvatar (Component)
Displays user informations, like avatar, first name, last name and registration date.
Selector
ack-avatar
Input
- user (User|string|null): the user object or an MD5 hash. If an empty value is supplied, the avatar is calculated on
unknown@gravatar.comfake e-mail address. Defaults tonull. - size (number): size for the user's avatar, in pixels. Defaults to
80. - alt (string): text for the avatar image
altattribute. - options (AckAvatarConfig): additional options to display the user's avatar.
/**
* 1. Import the module and all classes you need in your main module (usually app.module.ts).
*/
import { AckUserModule, AckUserConfig } from '@acknow-srl/user';
/**
* 2. Add the module to your app imports and configure it.
*/
const conf: AckUserConfig = {
server: 'http://my-graphql-server-url'
};
@NgModule({
declarations: [
AppComponent
...
],
imports: [
...
AckUserModule.config(conf),
...
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule {
}
/**
* 3. Import the AckUser and/or AckApp services in your components or services to use it.
*/
import { AckUser, User, AckApp, App } from '@acknow-srl/user';
@Component({
...
})
export class MyComponent implements OnInit {
me: User|null;
appData: App|null;
constructor(private userService: AckUser, private appService: AckApp) {
}
ngOnInit() {
// Sets the logged in user.
this.userService.me().subscribe(user => {
this.me = user;
// Sets app data based on the logged in user ID and app ID.
this.appService.get({userId: this.me.id, appId: 'my-app-id'}).subscribe(app => {
this.appData = app;
});
},
err => {
this.me = null;
this.appData = null;
});
}
}