All files / prefixAPI prefix-client.ts

77.77% Statements 70/90
80% Branches 8/10
83.33% Functions 5/6
77.77% Lines 70/90

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 911x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 97x 97x 97x 97x 2x 2x 2x 2x 2x 2x 1x 1x 10x 10x 10x 10x 10x 10x                                     1x  
// services/client.ts
import { ApolloClient, InMemoryCache } from "@apollo/client";
 
import * as queries from "./queries";
 
import * as vscode from "vscode";
 
export class PrefixClient {
	private static baseURL = "https://prefix.dev/api/graphql";
 
	private instance = new ApolloClient({
		uri: PrefixClient.baseURL,
		cache: new InMemoryCache(),
		headers: {
			Authorization:
				`Bearer ` +
					vscode.workspace
						.getConfiguration("pixi-vscode")
						.get("prefixAPIKey") || "",
		},
	});
 
	constructor() {}
 
	static getBaseURL() {
		return PrefixClient.baseURL;
	}
 
	async getAllChannels() {
		const result = await this.instance.query({
			query: queries.GET_USER_CHANNELS,
			variables: {
				page: 0,
			},
		});
 
		const totalPages = result.data.channels.pages;
		const allChannels: {
			name: string;
			description: string;
			baseUrl: string;
			owner: string;
		}[] = [];
 
		for (let i = 0; i < totalPages; i++) {
			const result = await this.instance.query({
				query: queries.GET_USER_CHANNELS,
				variables: {
					page: i,
				},
			});
			allChannels.push(
				...result.data.channels.page.map((channel: any) => ({
					name: channel.name,
					description: channel.description,
					baseUrl: channel.baseUrl,
					owner: channel.owner || "",
				}))
			);
		}
 
		return allChannels;
	}
 
	async getPackages(packageName: string) {
		const result = await this.instance.query({
			query: queries.FIND_PACKAGES,
			variables: {
				packageName: packageName,
			},
		});

		// extract the package names and their channel names
		const packageNames: {
			channel: string;
			package: string;
			summary: string;
			version: string;
		}[] = [];
		result.data.packages.page.flatMap((pkg: any) => {
			packageNames.push({
				channel: pkg.channel.name,
				package: pkg.name,
				summary: pkg.summary,
				version: pkg.latestVersion.version,
			});
		});
		return packageNames;
	}
}