@noatgnu/cupcake-core 1.2.17 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,121 +1,163 @@
1
- # CupcakeCore
1
+ # Cupcake Core
2
2
 
3
- A reusable Angular library that provides user management, authentication, and site configuration functionality for cupcake applications.
3
+ The `cupcake-core` library provides essential services, components, and models for building Angular applications that interact with a Cupcake backend. It simplifies authentication, API communication, and provides a set of reusable UI components.
4
4
 
5
- ## Features
5
+ ## Key Features
6
6
 
7
- - **Authentication**: Login/register components with ORCID support
8
- - **User Management**: Admin user management, lab groups, user profiles
9
- - **Site Configuration**: Configurable site settings and branding
10
- - **Guards & Interceptors**: Auth guard, admin guard, and HTTP interceptors
11
- - **Models & Services**: Complete set of models and services for user management
7
+ - **Authentication:** Simplified user authentication and session management.
8
+ - **API Service:** A robust API service with automatic request/response case transformation.
9
+ - **Guards & Interceptors:** Route guards and HTTP interceptors for securing your application.
10
+ - **Reusable Components:** A collection of pre-built components for common UI patterns.
11
+ - **Type-safe Models:** A comprehensive set of TypeScript interfaces for backend data structures.
12
12
 
13
13
  ## Installation
14
14
 
15
+ To install the `@noatgnu/cupcake-core` library, run the following command:
16
+
15
17
  ```bash
16
- npm install cupcake-core
18
+ npm install @noatgnu/cupcake-core
17
19
  ```
18
20
 
19
- ## Usage
21
+ Then, import the `CupcakeCoreModule` into your application's `AppModule` (or any other feature module):
22
+
23
+ ```typescript
24
+ import { CupcakeCoreModule } from '@noatgnu/cupcake-core';
25
+
26
+ @NgModule({
27
+ imports: [
28
+ // ... other modules
29
+ CupcakeCoreModule
30
+ ],
31
+ // ...
32
+ })
33
+ export class AppModule { }
34
+ ```
20
35
 
21
- ### 1. Configure in your app.config.ts
36
+ Additionally, you need to provide the `CUPCAKE_CORE_CONFIG` in your `AppModule` to configure the API URL:
22
37
 
23
38
  ```typescript
24
- import { ApplicationConfig } from '@angular/core';
25
- import { provideHttpClient, withInterceptors } from '@angular/common/http';
26
- import { authInterceptor, CUPCAKE_CORE_CONFIG } from 'cupcake-core';
39
+ import { CUPCAKE_CORE_CONFIG } from '@noatgnu/cupcake-core';
27
40
 
28
- export const appConfig: ApplicationConfig = {
41
+ @NgModule({
42
+ // ...
29
43
  providers: [
30
- { provide: CUPCAKE_CORE_CONFIG, useValue: { apiUrl: 'https://your-api.com' } },
31
- provideHttpClient(
32
- withInterceptors([authInterceptor])
33
- ),
34
- // ... other providers
44
+ {
45
+ provide: CUPCAKE_CORE_CONFIG,
46
+ useValue: { apiUrl: 'YOUR_API_URL_HERE' }
47
+ }
35
48
  ]
36
- };
49
+ })
50
+ export class AppModule { }
37
51
  ```
38
52
 
39
- ### 2. Use components in your routes
53
+ ## Services
40
54
 
41
- ```typescript
42
- import { Routes } from '@angular/router';
43
- import { LoginComponent, RegisterComponent, authGuard, adminGuard } from 'cupcake-core';
44
-
45
- export const routes: Routes = [
46
- { path: 'login', component: LoginComponent },
47
- { path: 'register', component: RegisterComponent },
48
- { path: 'protected', component: YourComponent, canActivate: [authGuard] },
49
- { path: 'admin', component: AdminComponent, canActivate: [authGuard, adminGuard] },
50
- ];
51
- ```
55
+ ### AuthService
52
56
 
53
- ### 3. Import services in your components
57
+ The `AuthService` handles user authentication, including login, logout, and token management. It uses JWT for authentication and stores tokens in local storage.
58
+
59
+ **Usage:**
54
60
 
55
61
  ```typescript
56
- import { Component, inject } from '@angular/core';
57
- import { AuthService, SiteConfigService, User } from 'cupcake-core';
58
-
59
- @Component({
60
- selector: 'app-example',
61
- template: \`
62
- <div *ngIf="currentUser">
63
- Welcome, {{ currentUser.first_name }}!
64
- </div>
65
- \`
66
- })
67
- export class ExampleComponent {
68
- private authService = inject(AuthService);
69
- private siteConfigService = inject(SiteConfigService);
70
-
71
- currentUser = this.authService.currentUser$;
72
- siteConfig = this.siteConfigService.config$;
62
+ import { AuthService } from '@noatgnu/cupcake-core';
63
+
64
+ @Component({ ... })
65
+ export class MyComponent {
66
+ constructor(private authService: AuthService) {
67
+ this.authService.isAuthenticated$.subscribe(isAuthenticated => {
68
+ // ...
69
+ });
70
+
71
+ this.authService.currentUser$.subscribe(user => {
72
+ // ...
73
+ });
74
+ }
75
+
76
+ login() {
77
+ this.authService.login('username', 'password').subscribe(response => {
78
+ // ...
79
+ });
80
+ }
81
+
82
+ logout() {
83
+ this.authService.logout().subscribe(() => {
84
+ // ...
85
+ });
86
+ }
73
87
  }
74
88
  ```
75
89
 
76
- ## Available Components
90
+ ### ApiService
77
91
 
78
- - `LoginComponent` - Login form with username/password and ORCID support
79
- - `RegisterComponent` - User registration form
80
- - `UserProfileComponent` - User profile management
81
- - `UserManagementComponent` - Admin user management
82
- - `LabGroupsComponent` - Lab group management
83
- - `SiteConfigComponent` - Site configuration management
84
- - `ToastContainerComponent` - Toast notification container
92
+ The `ApiService` is the primary service for communicating with the Cupcake backend. It provides methods for a wide range of functionalities, including user management, site configuration, and more.
85
93
 
86
- ## Available Services
94
+ **Automatic Case Transformation:**
87
95
 
88
- - `AuthService` - Authentication and user management
89
- - `ApiService` - API communication
90
- - `SiteConfigService` - Site configuration
91
- - `UserManagementService` - User management operations
92
- - `ToastService` - Toast notifications
93
- - `ResourceService` - Resource utilities
96
+ The `ApiService` automatically transforms request data from camelCase (frontend) to snake_case (backend) and response data from snake_case to camelCase. This eliminates the need for manual transformations in your components and services.
94
97
 
95
- ## Available Guards
98
+ **Usage:**
96
99
 
97
- - `authGuard` - Protect routes requiring authentication
98
- - `adminGuard` - Protect admin-only routes
100
+ ```typescript
101
+ import { ApiService } from '@noatgnu/cupcake-core';
102
+
103
+ @Component({ ... })
104
+ export class MyComponent {
105
+ constructor(private apiService: ApiService) {
106
+ this.apiService.getUsers().subscribe(response => {
107
+ // `response` is already transformed to camelCase
108
+ console.log(response.results);
109
+ });
110
+ }
111
+ }
112
+ ```
99
113
 
100
- ## Available Models
114
+ ## Guards
101
115
 
102
- All necessary TypeScript interfaces and enums for user management, authentication, and site configuration.
116
+ The library provides the following route guards:
103
117
 
104
- ## Building the Library
118
+ - `AuthGuard`: Ensures that a route can only be accessed by authenticated users.
119
+ - `AdminGuard`: Ensures that a route can only be accessed by admin users.
105
120
 
106
- To build the library:
121
+ **Usage (in your routing module):**
107
122
 
108
- ```bash
109
- ng build cupcake-core
123
+ ```typescript
124
+ import { AuthGuard, AdminGuard } from '@noatgnu/cupcake-core';
125
+
126
+ const routes: Routes = [
127
+ {
128
+ path: 'profile',
129
+ component: UserProfileComponent,
130
+ canActivate: [AuthGuard]
131
+ },
132
+ {
133
+ path: 'admin',
134
+ component: AdminDashboardComponent,
135
+ canActivate: [AdminGuard]
136
+ }
137
+ ];
110
138
  ```
111
139
 
112
- To publish:
140
+ ## Interceptors
113
141
 
114
- ```bash
115
- cd dist/cupcake-core
116
- npm publish
117
- ```
142
+ The `AuthInterceptor` automatically attaches the JWT access token to outgoing HTTP requests. It also handles token refreshing when the access token expires.
143
+
144
+ ## Components
145
+
146
+ The `cupcake-core` library includes a variety of reusable components, such as:
147
+
148
+ - **Login Form:** A complete login form with username/password fields.
149
+ - **User Management:** Components for listing, creating, and editing users.
150
+ - **Color Picker:** A color selection component.
151
+ - **Toast Notifications:** A container for displaying toast messages.
152
+ - **Powered-by Footer:** A standard footer component.
153
+
154
+ ## Models
155
+
156
+ The library defines a comprehensive set of TypeScript interfaces that correspond to the backend data models. These models provide type safety and autocompletion when working with API responses. Some of the key models include:
118
157
 
119
- ## Development
158
+ - `User`
159
+ - `SiteConfig`
160
+ - `Annotation`
161
+ - `ResourcePermission`
120
162
 
121
- The library is designed to be framework-agnostic and can be imported by any Angular application that needs user management functionality.
163
+ By leveraging the services, components, and models provided by `cupcake-core`, you can significantly accelerate the development of your Angular application and ensure consistency with the Cupcake backend.