Skip to content

Commit

Permalink
refactor: docs api endpoint from trpc to analogjs server call
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsamsonkid committed Dec 11, 2024
1 parent 7393eb0 commit f847689
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 31 deletions.
2 changes: 2 additions & 0 deletions apps/app/src/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ApplicationConfig } from '@angular/core';
import { provideClientHydration } from '@angular/platform-browser';
import { PreloadAllModules, withInMemoryScrolling, withNavigationErrorHandler, withPreloading } from '@angular/router';
import { provideTrpcClient } from './trpc-client';
import { provideHttpClient, withFetch } from '@angular/common/http';

export const appConfig: ApplicationConfig = {
providers: [
Expand All @@ -13,5 +14,6 @@ export const appConfig: ApplicationConfig = {
),
provideClientHydration(),
provideTrpcClient(),
provideHttpClient(withFetch()),
],
};
23 changes: 6 additions & 17 deletions apps/app/src/app/core/services/ui-docs.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { waitFor } from '@analogjs/trpc';
import { Injectable } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { shareReplay, Subject, switchMap } from 'rxjs';
import { injectTRPCClient } from '../../../trpc-client';
import { Primitives, PrimitiveSubTypes } from '../models/ui-docs.model';
import { computed, Injectable, signal } from '@angular/core';
import { ComponentApiData, Primitives, PrimitiveSubTypes } from '../models/ui-docs.model';

type SamePageAnchorLink = {
id: string;
Expand All @@ -13,18 +9,11 @@ type SamePageAnchorLink = {

@Injectable()
export class UIDocsService {
private readonly _trpc = injectTRPCClient();
private readonly _uiDocs = signal<ComponentApiData | null>(null);
public uiDocs = computed(() => this._uiDocs());

public triggerRefresh$ = new Subject<void>();
public uiDocs$ = this.triggerRefresh$.pipe(
switchMap(() => this._trpc.docs.list.query()),
shareReplay(1),
);
public uiDocs = toSignal(this.uiDocs$);

constructor() {
void waitFor(this.uiDocs$);
this.triggerRefresh$.next();
setAPIData(data: ComponentApiData): void {
this._uiDocs.set(data);
}

getPrimitiveDoc(primitive: Primitives): PrimitiveSubTypes | undefined {
Expand Down
15 changes: 13 additions & 2 deletions apps/app/src/app/pages/(components)/components.page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RouteMeta } from '@analogjs/router';
import { Component } from '@angular/core';
import { Component, inject } from '@angular/core';
import { provideIcons } from '@ng-icons/core';
import { lucideRocket } from '@ng-icons/lucide';
import {
Expand All @@ -12,6 +12,10 @@ import { HlmIconComponent } from '@spartan-ng/ui-icon-helm';
import { UIDocsService } from '../../core/services/ui-docs.service';
import { PageComponent } from '../../shared/layout/page.component';
import { metaWith } from '../../shared/meta/meta.util';
import { injectLoad } from '@analogjs/router';
import { load } from './components.server';
import { toSignal } from '@angular/core/rxjs-interop';
import { ComponentApiData } from '../../core/models/ui-docs.model';

export const routeMeta: RouteMeta = {
meta: metaWith(
Expand Down Expand Up @@ -53,4 +57,11 @@ export const routeMeta: RouteMeta = {
<spartan-page />
`,
})
export default class ComponentsPageComponent {}
export default class ComponentsPageComponent {
private readonly _apiData = toSignal(injectLoad<typeof load>(), { requireSync: true });
private readonly _uiDocsService = inject(UIDocsService);

constructor() {
this._uiDocsService.setAPIData(this._apiData() as ComponentApiData)
}
}
5 changes: 5 additions & 0 deletions apps/app/src/app/pages/(components)/components.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PageServerLoad } from '@analogjs/router';

export const load = async ({ fetch }: PageServerLoad) => {
return await fetch('/api/primitive-api');
};
6 changes: 6 additions & 0 deletions apps/app/src/server/routes/primitive-api.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineEventHandler } from 'h3';
import docsData from '../../public/data/ui-api.json';

export default defineEventHandler(() => {
return docsData;
});
9 changes: 0 additions & 9 deletions apps/app/src/server/trpc/routers/docs.ts

This file was deleted.

4 changes: 1 addition & 3 deletions apps/app/src/server/trpc/routers/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { router } from '../trpc';
import { docsRouter } from './docs';
import { noteRouter } from './notes';

export const appRouter = router({
note: noteRouter,
docs: docsRouter,
note: noteRouter
});
// export type definition of API
export type AppRouter = typeof appRouter;

0 comments on commit f847689

Please sign in to comment.