11import * as t from "io-ts" ;
2+ import { z } from "zod" ;
23import fetch , { Response } from "../../lib/fetch" ;
34import { fold } from "fp-ts/Either" ;
45
@@ -35,6 +36,7 @@ import {
3536 FrameworkWroomTelemetryID ,
3637 StarterId ,
3738 Environment ,
39+ OnboardingState ,
3840} from "./types" ;
3941import { sortEnvironments } from "./sortEnvironments" ;
4042
@@ -506,6 +508,115 @@ export class PrismicRepositoryManager extends BaseManager {
506508 }
507509 }
508510
511+ async fetchOnboarding ( ) : Promise < OnboardingState > {
512+ const repositoryName = await this . project . getRepositoryName ( ) ;
513+
514+ const url = new URL ( "/onboarding" , API_ENDPOINTS . RepositoryService ) ;
515+ url . searchParams . set ( "repository" , repositoryName ) ;
516+ const res = await this . _fetch ( { url } ) ;
517+
518+ if ( res . ok ) {
519+ const json = await res . json ( ) ;
520+ const { value, error } = decode ( OnboardingState , json ) ;
521+
522+ if ( error ) {
523+ throw new UnexpectedDataError (
524+ `Failed to decode onboarding: ${ error . errors . join ( ", " ) } ` ,
525+ ) ;
526+ }
527+ if ( value ) {
528+ return value ;
529+ }
530+ }
531+
532+ switch ( res . status ) {
533+ case 400 :
534+ case 401 :
535+ throw new UnauthenticatedError ( ) ;
536+ case 403 :
537+ throw new UnauthorizedError ( ) ;
538+ default :
539+ throw new Error ( "Failed to fetch onboarding." ) ;
540+ }
541+ }
542+
543+ async toggleOnboardingStep (
544+ stepId : string ,
545+ ) : Promise < { completedSteps : string [ ] } > {
546+ const repositoryName = await this . project . getRepositoryName ( ) ;
547+
548+ const url = new URL (
549+ `/onboarding/${ stepId } /toggle` ,
550+ API_ENDPOINTS . RepositoryService ,
551+ ) ;
552+ url . searchParams . set ( "repository" , repositoryName ) ;
553+ const res = await this . _fetch ( { url, method : "PATCH" } ) ;
554+
555+ if ( res . ok ) {
556+ const json = await res . json ( ) ;
557+ const { value, error } = decode (
558+ z . object ( { completedSteps : z . array ( z . string ( ) ) } ) ,
559+ json ,
560+ ) ;
561+
562+ if ( error ) {
563+ throw new UnexpectedDataError (
564+ `Failed to decode onboarding step toggle: ${ error . errors . join ( ", " ) } ` ,
565+ ) ;
566+ }
567+
568+ if ( value ) {
569+ return value ;
570+ }
571+ }
572+
573+ switch ( res . status ) {
574+ case 400 :
575+ case 401 :
576+ throw new UnauthenticatedError ( ) ;
577+ case 403 :
578+ throw new UnauthorizedError ( ) ;
579+ default :
580+ throw new Error ( "Failed to toggle onboarding step." ) ;
581+ }
582+ }
583+
584+ async toggleOnboarding ( ) : Promise < { isDismissed : boolean } > {
585+ const repositoryName = await this . project . getRepositoryName ( ) ;
586+
587+ const url = new URL ( "/onboarding/toggle" , API_ENDPOINTS . RepositoryService ) ;
588+ url . searchParams . set ( "repository" , repositoryName ) ;
589+ const res = await this . _fetch ( { url, method : "PATCH" } ) ;
590+
591+ if ( res . ok ) {
592+ const json = await res . json ( ) ;
593+ const { value, error } = decode (
594+ z . object ( { isDismissed : z . boolean ( ) } ) ,
595+ json ,
596+ ) ;
597+
598+ if ( error ) {
599+ throw new UnexpectedDataError (
600+ `Failed to decode onboarding toggle: ${ error . errors . join ( ", " ) } ` ,
601+ ) ;
602+ }
603+
604+ if ( value ) {
605+ return value ;
606+ }
607+ }
608+
609+ switch ( res . status ) {
610+ case 400 :
611+ case 401 :
612+ throw new UnauthenticatedError ( ) ;
613+ case 403 :
614+ throw new UnauthorizedError ( ) ;
615+ default :
616+ throw new Error ( "Failed to toggle onboarding guide." ) ;
617+ }
618+ }
619+
509620 private _decodeLimitOrThrow (
510621 potentialLimit : unknown ,
511622 statusCode : number ,
@@ -531,7 +642,7 @@ export class PrismicRepositoryManager extends BaseManager {
531642
532643 private async _fetch ( args : {
533644 url : URL ;
534- method ?: "GET" | "POST" ;
645+ method ?: "GET" | "POST" | "PATCH" ;
535646 body ?: unknown ;
536647 userAgent ?: PrismicRepositoryUserAgents ;
537648 repository ?: string ;
0 commit comments