The NestJS API serves the public profile through Apollo Sandbox and loads only the relations selected by the query.
34 lines
888 B
TypeScript
34 lines
888 B
TypeScript
import { Inject, Injectable } from '@nestjs/common';
|
|
import {
|
|
InvalidProfileSlugError,
|
|
ProfileNotFoundError,
|
|
} from '../domain/errors.js';
|
|
import type { PublicProfile } from '../domain/profile.js';
|
|
import type { RelationPlan } from '../domain/relation-plan.js';
|
|
import {
|
|
PROFILE_REPOSITORY,
|
|
type ProfileRepository,
|
|
} from './profile.repository.js';
|
|
|
|
const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
|
|
@Injectable()
|
|
export class ProfileService {
|
|
constructor(
|
|
@Inject(PROFILE_REPOSITORY) private readonly profiles: ProfileRepository,
|
|
) {}
|
|
|
|
async getBySlug(slug: string, plan: RelationPlan): Promise<PublicProfile> {
|
|
if (slug.length > 64 || !SLUG.test(slug)) {
|
|
throw new InvalidProfileSlugError(slug);
|
|
}
|
|
|
|
const profile = await this.profiles.findBySlug(slug, plan);
|
|
if (!profile) {
|
|
throw new ProfileNotFoundError(slug);
|
|
}
|
|
|
|
return profile;
|
|
}
|
|
}
|