@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 +124 -82
- package/fesm2022/noatgnu-cupcake-core.mjs +516 -401
- package/fesm2022/noatgnu-cupcake-core.mjs.map +1 -1
- package/index.d.ts +158 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,121 +1,163 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cupcake Core
|
|
2
2
|
|
|
3
|
-
|
|
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
|
|
8
|
-
- **
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
- **Models
|
|
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
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
-
|
|
41
|
+
@NgModule({
|
|
42
|
+
// ...
|
|
29
43
|
providers: [
|
|
30
|
-
{
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
53
|
+
## Services
|
|
40
54
|
|
|
41
|
-
|
|
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
|
-
|
|
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 {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
90
|
+
### ApiService
|
|
77
91
|
|
|
78
|
-
|
|
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
|
-
|
|
94
|
+
**Automatic Case Transformation:**
|
|
87
95
|
|
|
88
|
-
|
|
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
|
-
|
|
98
|
+
**Usage:**
|
|
96
99
|
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
##
|
|
114
|
+
## Guards
|
|
101
115
|
|
|
102
|
-
|
|
116
|
+
The library provides the following route guards:
|
|
103
117
|
|
|
104
|
-
|
|
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
|
-
|
|
121
|
+
**Usage (in your routing module):**
|
|
107
122
|
|
|
108
|
-
```
|
|
109
|
-
|
|
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
|
-
|
|
140
|
+
## Interceptors
|
|
113
141
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
158
|
+
- `User`
|
|
159
|
+
- `SiteConfig`
|
|
160
|
+
- `Annotation`
|
|
161
|
+
- `ResourcePermission`
|
|
120
162
|
|
|
121
|
-
|
|
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.
|