An open API service indexing awesome lists of open source software.

https://github.com/beeefriends/beefriends-shared-kernel

Shared Kernel Libary BeeFriends 📝
https://github.com/beeefriends/beefriends-shared-kernel

shared-kernel

Last synced: about 1 month ago
JSON representation

Shared Kernel Libary BeeFriends 📝

Awesome Lists containing this project

README

          

# @beefriends/shared-kernel

Shared TypeScript types and endpoint constants for BeeFriends. The frontend should import API contracts from this package instead of copying backend DTO shapes by hand.

## Installation

```bash
npm install @beefriends/shared-kernel
```

## Usage

```typescript
import {
AUTH_ENDPOINTS,
CAMPUS_ENDPOINTS,
HOBBY_ENDPOINTS,
MAJOR_ENDPOINTS,
USER_ENDPOINTS,
} from '@beefriends/shared-kernel';

import type {
AuthResponseDto,
LoginDto,
RegisterDto,
UpdateUserDto,
UserProfileDto,
} from '@beefriends/shared-kernel';

// Backend Nest apps should import validation DTO classes from:
// import { RegisterDto } from "@beefriends/shared-kernel/dto";

const registerPayload: RegisterDto = {
binusianEmail: 'adrian001@binus.ac.id',
password: 'password123',
phoneNumber: '+6281234567890',
displayName: 'Adrian',
binusianYear: 2024,
majorId: 1,
campusId: 1,
hobbyIds: [1, 2, 3],
description: 'Computer Science student who loves coffee.',
};

const formData = new FormData();
Object.entries(registerPayload).forEach(([key, value]) => {
formData.append(
key,
Array.isArray(value) ? JSON.stringify(value) : String(value),
);
});
formData.append('profilePhoto', profilePhotoFile);
galleryPhotoFiles.forEach((file) => formData.append('photos', file));

await fetch(AUTH_ENDPOINTS.REGISTER, { method: 'POST', body: formData });

const loginPayload: LoginDto = { idToken: 'firebase-id-token' };
const loginResponse: AuthResponseDto = await fetch(AUTH_ENDPOINTS.LOGIN, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(loginPayload),
}).then((response) => response.json());

const me: UserProfileDto = loginResponse.user;
```

## Endpoints

| Constant | Endpoints |
| ------------------ | ------------------------------------------------------ |
| `AUTH_ENDPOINTS` | `REGISTER`, `LOGIN` |
| `USER_ENDPOINTS` | `ME`, `BY_ID(id)` |
| `CAMPUS_ENDPOINTS` | `LIST` (`/api/v1/user/campus`), `BY_ID(id)`, `CREATE` |
| `MAJOR_ENDPOINTS` | `LIST` (`/api/v1/user/majors`), `BY_ID(id)`, `CREATE` |
| `HOBBY_ENDPOINTS` | `LIST` (`/api/v1/user/hobbies`), `BY_ID(id)`, `CREATE` |

## Types

| Type | Description |
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `RegisterDto` | Text fields for multipart `POST /api/v1/auth/register`; attach `profilePhoto` and optional repeated `photos` files separately |
| `LoginDto` | `{ idToken }` from Firebase client sign-in |
| `AuthResponseDto` | BeeFriends JWT and normalized user profile |
| `UserProfileDto` | Public user profile shape returned by auth/user endpoints |
| `UpdateUserDto` | Partial profile fields for `PATCH /api/v1/users/me` |
| `CampusDto` | Normalized campus shape inside profile responses |
| `MajorDto` | Normalized major shape inside profile responses |
| `HobbyDto` | Normalized hobby shape inside profile responses |
| `CreateCampusDto` | Payload for creating campus master data |
| `CreateMajorDto` | Payload for creating major master data |
| `CreateHobbyDto` | Payload for creating hobby master data |

`DEPARTMENT_ENDPOINTS`, `DepartmentDto`, and `CreateDepartmentDto` are kept as compatibility aliases for older frontend code. Prefer the `Major` names for new code.

## Nest DTO Classes

Backend services can import decorator-backed DTO classes from the `dto` subpath:

```typescript
import { LoginDto, RegisterDto } from '@beefriends/shared-kernel/dto';
```

The root package stays frontend-safe and only exports plain types plus endpoint constants.

## Versioning

```bash
npm run release:patch
npm run release:minor
npm run release:major
```

Pushing a tag triggers GitHub Actions to build and publish to npm automatically.