All files / owid-grapher/gitCms GitCmsClient.ts

92.65% Statements 63/68
100% Branches 6/6
85.71% Functions 6/7
92.65% Lines 63/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 741x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 3x 3x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 1x            
import {
    GIT_CMS_READ_ROUTE,
    WriteRequest,
    ReadRequest,
    GitCmsResponse,
    GitCmsReadResponse,
    DeleteRequest,
    GIT_CMS_PULL_ROUTE,
    GitPullResponse,
    GlobRequest,
    GitCmsGlobResponse,
    GIT_CMS_GLOB_ROUTE,
    GIT_CMS_DELETE_ROUTE,
    GIT_CMS_WRITE_ROUTE,
} from "./GitCmsConstants"
 
// todo: clarify what is going on here. i already forget.
const validateFilePath = (path: string) => {
    if (path.includes("~")) throw new Error(`Filenames with ~ not supported`)
}
 
export class GitCmsClient {
    private basePath: string
    constructor(basePath: string) {
        this.basePath = basePath
    }
 
    async pullFromGithub() {
        const response = await fetch(`${this.basePath}${GIT_CMS_PULL_ROUTE}`, {
            method: "POST",
        })
        return (await response.json()) as GitPullResponse
    }
 
    async readRemoteFiles(request: GlobRequest) {
        return (await this.post(
            GIT_CMS_GLOB_ROUTE,
            request
        )) as GitCmsGlobResponse
    }
 
    private async post(route: string, request: any) {
        const response = await fetch(`${this.basePath}${route}`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(request),
        })
        return await response.json()
    }
 
    async deleteRemoteFile(request: DeleteRequest) {
        validateFilePath(request.filepath)
        return (await this.post(
            GIT_CMS_DELETE_ROUTE,
            request
        )) as GitCmsResponse
    }
 
    async readRemoteFile(request: ReadRequest) {
        validateFilePath(request.filepath)
        return (await this.post(
            GIT_CMS_READ_ROUTE,
            request
        )) as GitCmsReadResponse
    }
 
    async writeRemoteFile(request: WriteRequest) {
        validateFilePath(request.filepath)
        return (await this.post(GIT_CMS_WRITE_ROUTE, request)) as GitCmsResponse
    }
}