Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination support #11

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"devDependencies": {
"@types/chai": "^4.3",
"@types/mocha": "^5.2.5",
"@types/nock": "^9.3.0",
"@types/node": "^20.14.8",
"@types/node-fetch": "^2.1.2",
"chai": "^4.3",
Expand Down
16 changes: 8 additions & 8 deletions src/buildpack-registry-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch, {Headers, RequestInit, Response} from 'node-fetch'
import fetch, { Headers, RequestInit, Response } from 'node-fetch'

export {Response} from 'node-fetch'
export { Response } from 'node-fetch'

export type IBody = {
[property: string]: string
Expand Down Expand Up @@ -85,7 +85,7 @@ export class BuildpackRegistryApi {
}

headers(options: HeaderOptions = {}): Headers {
let defaultHeaders: {[property: string]: string} = {
let defaultHeaders: { [property: string]: string } = {
Accept: 'application/vnd.heroku+json; version=3.buildpack-registry',
'Content-Type': 'application/json'
}
Expand All @@ -99,7 +99,7 @@ export class BuildpackRegistryApi {

if (process.env.HEROKU_HEADERS) {
let herokuHeaders = JSON.parse(process.env.HEROKU_HEADERS)
return new Headers({...defaultHeaders, herokuHeaders})
return new Headers({ ...defaultHeaders, herokuHeaders })
} else {
return new Headers(defaultHeaders)
}
Expand All @@ -117,9 +117,9 @@ export class BuildpackRegistryApi {
return fetch(`${BuildpackRegistryApi.url()}${path}`, options)
}

async get(path: string): Promise<Response> {
return fetch(`${BuildpackRegistryApi.url()}${path}`, {
headers: this.headers()
})
async get(path: string, headers?: Headers): Promise<Response> {
return await fetch(`${BuildpackRegistryApi.url()}${path}`, {
headers: headers ? headers : this.headers(),
});
}
}
65 changes: 37 additions & 28 deletions src/buildpack-registry.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {Result} from 'true-myth'
import { Result } from 'true-myth'

import {BuildpackBody, BuildpackRegistryApi as Api, Category, HeaderOptions, ReadmeBody, Response, RevisionBody, RevisionStatus} from './buildpack-registry-api'
import { BuildpackBody, BuildpackRegistryApi as Api, Category, HeaderOptions, ReadmeBody, Response, RevisionBody, RevisionStatus } from './buildpack-registry-api'

export {BuildpackBody, Category, ReadmeBody, RevisionBody, RevisionStatus}
import { Headers } from 'node-fetch'

export { BuildpackBody, Category, ReadmeBody, RevisionBody, RevisionStatus }

const BUILDPACK_FORMATTING_MESSAGE = "To specify a buildpack, please format it like the following: namespace/name (e.g. heroku/ruby). Also names can only contain letters, numbers, '_', and '-'."

Expand Down Expand Up @@ -58,15 +60,41 @@ export class BuildpackRegistry {
}
}

async list(path: string): Promise<Result<Array<any>, ResponseError>> {
let page = await this.api.get(path);
let items = [];
if (page.status === 200 || page.status === 206) {
items = await page.json();
}
while (page.status === 206) {
let nextRange = page.headers.get('Next-Range')
if (typeof nextRange !== "string") {
break;
}
page = await this.api.get(path, new Headers({ 'Accept-Range': nextRange}));
if (page.status === 200 || page.status === 206) {
items = [...items, ...await page.json()];
}
}
if (page.status !== 200) {
return Result.err({
status: page.status,
path,
description: await page.text()
});
}
return Result.ok(items);
}

async publish(buildpack: string, ref: string, token: string, secondFactor?: string): Promise<Result<RevisionBody, ResponseError>> {
let options: HeaderOptions = {token}
let options: HeaderOptions = { token }
if (secondFactor !== undefined) {
options.secondFactor = secondFactor
}
let path = `/buildpacks/${encodeURIComponent(buildpack)}/revisions`
let response = await this.api.post(
path,
{ref},
{ ref },
this.api.headers(options))

if (response.status === 200) {
Expand All @@ -81,7 +109,7 @@ export class BuildpackRegistry {
}

async rollback(buildpack: string, token: string, secondFactor?: string): Promise<Result<RevisionBody, ResponseError>> {
let options: HeaderOptions = {token}
let options: HeaderOptions = { token }
if (secondFactor !== undefined) {
options.secondFactor = secondFactor
}
Expand Down Expand Up @@ -123,16 +151,7 @@ export class BuildpackRegistry {
}

let path = `/buildpacks${queryString}`
let response = await this.api.get(path)
if (response.status === 200) {
return Result.ok(await response.json())
} else {
return Result.err({
status: response.status,
path,
description: await response.text(),
})
}
return this.list(path);
}

async info(buildpack: string): Promise<Result<InfoData, ResponseError>> {
Expand Down Expand Up @@ -196,7 +215,7 @@ export class BuildpackRegistry {
}

async archive(buildpack: string, token: string, secondFactor?: string): Promise<Result<BuildpackBody, ResponseError>> {
let options: HeaderOptions = {token}
let options: HeaderOptions = { token }
if (secondFactor !== undefined) {
options.secondFactor = secondFactor
}
Expand Down Expand Up @@ -233,17 +252,7 @@ export class BuildpackRegistry {

async listVersions(buildpack: string): Promise<Result<RevisionBody[], ResponseError>> {
let path = `/buildpacks/${encodeURIComponent(buildpack)}/revisions`
let response = await this.api.get(path)

if (response.status === 200) {
return Result.ok(await response.json())
} else {
return Result.err({
status: response.status,
path,
description: await response.text()
})
}
return await this.list(path)
}

async delay(ms: number) {
Expand Down
13 changes: 8 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
version "5.2.5"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.5.tgz#8a4accfc403c124a0bafe8a9fc61a05ec1032073"

"@types/nock@^9.3.0":
version "9.3.1"
resolved "https://registry.yarnpkg.com/@types/nock/-/nock-9.3.1.tgz#7d761a43a10aebc7ec6bae29d89afc6cbffa5d30"
integrity sha512-eOVHXS5RnWOjTVhu3deCM/ruy9E6JCgeix2g7wpFiekQh3AaEAK1cz43tZDukKmtSmQnwvSySq7ubijCA32I7Q==
dependencies:
"@types/node" "*"

"@types/node-fetch@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.1.2.tgz#8c5da14d70321e4c4ecd5db668e3f93cf6c7399f"
Expand Down Expand Up @@ -1650,11 +1657,7 @@ tsutils@^2.27.2:
dependencies:
tslib "^1.8.1"

type-detect@^4.0.0:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"

type-detect@^4.1.0:
type-detect@^4.0.0, type-detect@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.1.0.tgz#deb2453e8f08dcae7ae98c626b13dddb0155906c"
integrity sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==
Expand Down
Loading