11import { computed } from "vue" ;
2- import { createSharedComposable , isObject , useLocalStorage } from "@vueuse/core" ;
2+ import { createSharedComposable , useLocalStorage } from "@vueuse/core" ;
33import dayjs from "dayjs" ;
4+ import { isExportedRepository , type ExportedRepository } from "@/helpers/export" ;
45import { fetchRepo , fetchRepositoryFiles , fetchRepositoryPackages , fetchRepositoryWorkflows } from "@/service/octokit" ;
56import type { Repository } from "@/composable/useRepo" ;
67
@@ -9,11 +10,6 @@ interface RepositoriesStore {
910 data : Repository [ ]
1011} ;
1112
12- export type ExportedRepository = Pick < Repository , "id" | "full_name" | "integrations" > ;
13- export function isExportedRepository ( data : unknown ) : data is ExportedRepository {
14- return isObject ( data ) && "id" in data && "full_name" in data && "integrations" in data ;
15- }
16-
1713const DEFAULT_STORE : RepositoriesStore = {
1814 lastUpdate : dayjs ( ) . toISOString ( ) ,
1915 data : [ ]
@@ -49,7 +45,10 @@ export const useRepositoriesStore = createSharedComposable(() => {
4945 return repositories . value . some ( ( { id : repoId } ) => repoId === id ) ;
5046 }
5147
52- async function addRepository ( fullName : Repository [ "full_name" ] , integrations : Repository [ "integrations" ] = { } ) : Promise < void > {
48+ async function addRepository (
49+ fullName : Repository [ "full_name" ] ,
50+ integrations : Repository [ "integrations" ] = { }
51+ ) : Promise < void > {
5352 const repo = await fetchRepo ( fullName ) ;
5453 if ( ! repo ) throw new Error ( "Repo not found" ) ;
5554 if ( isRepoExists ( repo . id ) ) return updateRepository ( fullName , integrations ) ;
@@ -65,7 +64,10 @@ export const useRepositoriesStore = createSharedComposable(() => {
6564 repositories . value = repositories . value . filter ( ( repo ) => repo . id !== id ) ;
6665 }
6766
68- async function updateRepository ( fullName : Repository [ "full_name" ] , integrations : Repository [ "integrations" ] ) : Promise < void > {
67+ async function updateRepository (
68+ fullName : Repository [ "full_name" ] ,
69+ integrations : Repository [ "integrations" ]
70+ ) : Promise < void > {
6971 const repo = await fetchRepo ( fullName ) ;
7072 if ( ! repo ) throw new Error ( "Repo not found" ) ;
7173
@@ -85,17 +87,20 @@ export const useRepositoriesStore = createSharedComposable(() => {
8587 }
8688
8789 async function importRepositories ( payload : ExportedRepository [ ] ) : Promise < void > {
88- const fetchPromises = payload . map ( ( { id, full_name, integrations } ) => isRepoExists ( id ) ?
89- updateRepository ( full_name , integrations ) :
90- addRepository ( full_name , integrations )
91- ) ;
90+ const fetchPromises = payload . reduce ( ( acc : Promise < void > [ ] , repo ) => {
91+ if ( isExportedRepository ( repo ) ) {
92+ const { id, full_name, integrations } = repo ;
93+ const request = isRepoExists ( id ) ?
94+ updateRepository ( full_name , integrations ) :
95+ addRepository ( full_name , integrations ) ;
96+ acc . push ( request ) ;
97+ }
98+ return acc ;
99+ } , [ ] ) ;
92100 await Promise . all ( fetchPromises ) ;
93101 }
94- function exportRepositories ( ) : string {
95- const repos : ExportedRepository [ ] = repositories . value . map (
96- ( { id, full_name, integrations } ) => ( { id, full_name, integrations } )
97- ) ;
98- return JSON . stringify ( repos , null , 2 ) ;
102+ function exportRepositories ( ) : ExportedRepository [ ] {
103+ return repositories . value . map ( ( { id, full_name, integrations } ) => ( { id, full_name, integrations } ) ) ;
99104 }
100105
101106 function updateCheck ( ) {
0 commit comments