Skip to content

Commit

Permalink
feat(nodejs): update mock data logic and use cookie (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexf00 authored Oct 10, 2024
1 parent 072030f commit d5e5e3d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 23 deletions.
31 changes: 31 additions & 0 deletions nodejs-usip/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions nodejs-usip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@nestjs/common": "^10.3.2",
"@nestjs/core": "^10.3.2",
"@nestjs/platform-express": "^10.3.2",
"cookie-parser": "^1.4.7",
"reflect-metadata": "^0.2.1",
"rxjs": "^7.8.1"
},
Expand All @@ -26,6 +27,7 @@
"@nestjs/testing": "^10.3.2",
"@swc/cli": "^0.3.9",
"@swc/core": "^1.4.0",
"@types/cookie-parser": "^1.4.7",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
Expand Down
5 changes: 3 additions & 2 deletions nodejs-usip/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Controller, Get, Headers, Query, Post, Body } from '@nestjs/common';
import { Controller, Get, Query, Post, Body } from '@nestjs/common';
import { AppService } from './app.service';
import { Cookies } from './decorator/cookies.decorator';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get('credential')
verifyCredential(@Headers("x-authorization") token: string): any {
verifyCredential(@Cookies('x-authorization') token: string): any {
let userId = this.appService.verifyCredential(token);
if (!userId) {
return { error: "Invalid token" };
Expand Down
63 changes: 42 additions & 21 deletions nodejs-usip/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,20 @@ export class Member {

@Injectable()
export class AppService {
// Mock data
xAuthorizations = {
"token:1": "1",
"token:2": "2",
"token:3": "3",
"token:4": "4",
}

// Mock data
users = [
new User("1", "Alice", "https://example.com/alice.jpg"),
new User("2", "Bob", "https://example.com/bob.jpg"),
new User("3", "Charlie", "https://example.com/charlie.jpg"),
]

members = [
new Member("unit1", "1", Role.Owner),
new Member("unit1", "2", Role.Editor),
new Member("unit2", "2", Role.Owner),
new Member("unit2", "3", Role.Editor),
new Member("unit3", "3", Role.Owner),
new Member("unit3", "1", Role.Reader),
new User("1", "Alice", this.generateAvatar("Alice")),
new User("2", "Bob", this.generateAvatar("Bob")),
new User("3", "Charlie", this.generateAvatar("Charlie")),
new User("4", "David", this.generateAvatar("David")),
]


Expand All @@ -76,21 +71,47 @@ export class AppService {
}

getRole(unitId: string, userId: string): Member {
for (let member of this.members) {
if (member.unitId === unitId && member.userId === userId) {
return member;
}
// TODO: implement this with real data
const user = this.getUser(userId);

// Mock data
if(user.name === "Alice") {
return new Member(unitId, userId, Role.Owner);
}
if(user.name === "Bob" || user.name === "David") {
return new Member(unitId, userId, Role.Editor);
}
if(user.name === "Charlie") {
return new Member(unitId, userId, Role.Reader);
}
return null;
}

getMembers(unitId: string): Member[] {
// TODO: implement this with real data

// Mock data
let members = [];
for (let member of this.members) {
if (member.unitId === unitId) {
members.push(member);
}
}
members.push(new Member(unitId, "1", Role.Owner));
members.push(new Member(unitId, "2", Role.Editor));
members.push(new Member(unitId, "3", Role.Reader));
members.push(new Member(unitId, "4", Role.Editor));
return members;
}

generateAvatar(name: string): string {
const color = `#${((name.charCodeAt(0) * 12345) & 0xffffff).toString(16).padStart(6, '0')}`;

const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
<rect width="50" height="50" fill="${color}" />
<text x="50%" y="50%" dy=".1em" fill="white" font-family="Arial" font-size="20" text-anchor="middle" dominant-baseline="middle">
${name.slice(0, 3)}
</text>
</svg>
`;

const base64 = Buffer.from(svg).toString('base64');
return `data:image/svg+xml;base64,${base64}`;
}
}
8 changes: 8 additions & 0 deletions nodejs-usip/src/decorator/cookies.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const Cookies = createParamDecorator(
(data: string, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return data ? request.cookies?.[data] : request.cookies;
},
);
5 changes: 5 additions & 0 deletions nodejs-usip/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConsoleLogger } from '@nestjs/common';
import * as cookieParser from 'cookie-parser';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

// https://docs.nestjs.com/techniques/cookies
app.use(cookieParser());

await app.listen(8080, () => {
new ConsoleLogger().log('Server is running on http://localhost:8080');
});
Expand Down

0 comments on commit d5e5e3d

Please sign in to comment.