Skip to content

Commit

Permalink
polishin data contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
natserract committed Nov 5, 2024
1 parent 53ce6a2 commit 8c07dec
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/components/src/apis/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export class BaseClient {
return (await this.api.get<T>(`${endpoint}/${identifier || identifier === 0 ? identifier : ''}`)).data
}

protected async getResources<T>(endpoint: string, params: object = {}): Promise<T[]> {
protected async getResources<T>(endpoint: string, params: object = {}): Promise<T> {
return (
await this.api.get<T[]>(endpoint, {
await this.api.get<T>(endpoint, {
params: { ...params },
})
).data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { BaseClient } from './client'
import { ENDPOINTS } from '../constants/endpoints'
import { VideoAttributesSchema, VideoAttributes } from '@toktik/contracts'
import { FeedsAPI } from '@toktik/contracts'

export class VideosAPI extends BaseClient {
public async searchVideos(keywords: string, count: number): Promise<VideoAttributes[]> {
return this.getResources(`${ENDPOINTS.VIDEOS}/search`, {
public async searchVideos(keywords: string, count: number): Promise<FeedsAPI.SearchResponse> {
return this.getResources<FeedsAPI.SearchResponse>(`${ENDPOINTS.FEEDS}/search`, {
keywords,
count,
})
}

public async getVideoById(id: string): Promise<any> {
return this.getResource(ENDPOINTS.VIDEOS, id)
return this.getResource(ENDPOINTS.FEEDS, id)
}

public async getStreamVideoById(id: string): Promise<any> {
return this.getResource(`${ENDPOINTS.VIDEOS}/stream`, id)
return this.getResource(`${ENDPOINTS.FEEDS}/stream`, id)
}
}
const videosAPI = new VideosAPI()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ref, onMounted } from 'vue'
import VLazyImage from 'v-lazy-image'
import SocialWallVideoView from '@components/social-wall/SocialWallVideoView.vue'
import videosAPI from '@apis/videos'
import videosAPI from '@apis/feeds'
const props = defineProps<{
id?: string
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/constants/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const ENDPOINTS = {
VIDEOS: '/feeds',
FEEDS: '/feeds',
RECOMMENDATIONS: '/recommendations',
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AuthorSchema = z.object({
avatar: z.string(),
})

export const VideoAttributesSchema = z.object({
export const FeedsAttributesSchema = z.object({
id: z.string().optional(),
aweme_id: z.string(),
video_id: z.string(),
Expand Down Expand Up @@ -58,4 +58,4 @@ export const VideoAttributesSchema = z.object({
is_top: z.number(),
})

export type VideoAttributes = z.infer<typeof VideoAttributesSchema>
export type FeedsAttributes = z.infer<typeof FeedsAttributesSchema>
25 changes: 25 additions & 0 deletions packages/contracts/src/v1/feeds/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { z } from 'zod'
import { FeedsAttributesSchema, FeedsAttributes } from '../common/feeds'

// Get
export const GetResponseSchema = z.object({
data: FeedsAttributesSchema,
})
export type GetResponse = z.infer<typeof GetResponseSchema>

export const GetRequestSchema = z.object({
id: z.string(),
})
export type GetRequest = z.infer<typeof GetRequestSchema>

// Search
export const SearchResponseSchema = z.object({
data: z.array(FeedsAttributesSchema),
})
export type SearchResponse = z.infer<typeof SearchResponseSchema>

export const SearchRequestSchema = z.object({
keywords: z.string(),
count: z.string(),
})
export type SearchRequest = z.infer<typeof SearchRequestSchema>
5 changes: 4 additions & 1 deletion packages/contracts/src/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './common/videos'
export * from './common/feeds'
export * as FeedsAPI from './feeds/api'
export * as RecommendationTagsAPI from './recommendations/tags/api'
export * as RecommendationKeywordsAPI from './recommendations/keywords/api'
9 changes: 9 additions & 0 deletions packages/contracts/src/v1/recommendations/keywords/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Get
import { z } from 'zod'

export const GetResponseSchema = z.object({
data: z.object({
keywords: z.array(z.string()),
}),
})
export type GetResponse = z.infer<typeof GetResponseSchema>
9 changes: 9 additions & 0 deletions packages/contracts/src/v1/recommendations/tags/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Get
import { z } from 'zod'

export const GetResponseSchema = z.object({
data: z.object({
tags: z.array(z.string()),
}),
})
export type GetResponse = z.infer<typeof GetResponseSchema>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dtos

type GetRecommendationsData struct {
Titles []string `json:"titles"`
Keywords []string `json:"keywords"`
}

type GetRecommendationsResponseDTO struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *GetRecommendationKeywordsHandler) Handle(

return &dtos.GetRecommendationsResponseDTO{
Data: dtos.GetRecommendationsData{
Titles: helper.SafeSubslice(titles, 3),
Keywords: helper.SafeSubslice(titles, 3),
},
}, nil
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (c *GetRecommendationKeywordsHandler) Handle(
log.Println("Found title similarities: ", titles, titleSimilarityScores)
result = &dtos.GetRecommendationsResponseDTO{
Data: dtos.GetRecommendationsData{
Titles: helper.SafeSubslice(titles, 3),
Keywords: helper.SafeSubslice(titles, 3),
},
}
}
Expand Down

0 comments on commit 8c07dec

Please sign in to comment.