Skip to content

Commit 304bf01

Browse files
authored
fix(command-link): retrieve all sites using pagination params (#1899)
1 parent 75742ee commit 304bf01

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

src/commands/link.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const process = require('process')
44
const { flags: flagsLib } = require('@oclif/command')
55
const chalk = require('chalk')
66

7+
const { listSites } = require('../lib/api')
78
const Command = require('../utils/command')
89
const { ensureNetlifyIgnore } = require('../utils/gitignore')
910
const linkPrompt = require('../utils/link/link-by-prompt')
@@ -77,9 +78,12 @@ class LinkCommand extends Command {
7778
if (flags.name) {
7879
let results
7980
try {
80-
results = await api.listSites({
81-
name: flags.name,
82-
filter: 'all',
81+
results = await listSites({
82+
api,
83+
options: {
84+
name: flags.name,
85+
filter: 'all',
86+
},
8387
})
8488
} catch (error) {
8589
if (error.status === 404) {

src/commands/sites/list.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { flags: flagsLib } = require('@oclif/command')
22
const chalk = require('chalk')
33
const { cli } = require('cli-ux')
44

5+
const { listSites } = require('../../lib/api')
56
const Command = require('../../utils/command')
67

78
class SitesListCommand extends Command {
@@ -20,7 +21,7 @@ class SitesListCommand extends Command {
2021
},
2122
})
2223

23-
const sites = await api.listSites({ filter: 'all' })
24+
const sites = await listSites({ api, options: { filter: 'all' } })
2425
if (!flags.json) {
2526
cli.action.stop()
2627
}

src/lib/api.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,17 @@ const cancelDeploy = async ({ api, deployId, warn }) => {
7777
}
7878
}
7979

80-
module.exports = { uploadEdgeHandlers, cancelDeploy }
80+
const FIRST_PAGE = 1
81+
const MAX_PAGES = 10
82+
const MAX_PER_PAGE = 100
83+
const listSites = async ({ api, options }) => {
84+
const { page = FIRST_PAGE, maxPages = MAX_PAGES, ...rest } = options
85+
const sites = await api.listSites({ page, per_page: MAX_PER_PAGE, ...rest })
86+
// TODO: use pagination headers when js-client returns them
87+
if (sites.length === MAX_PER_PAGE && page + 1 <= maxPages) {
88+
return [...sites, ...(await listSites({ api, options: { page: page + 1, maxPages, ...rest } }))]
89+
}
90+
return sites
91+
}
92+
93+
module.exports = { uploadEdgeHandlers, cancelDeploy, listSites }

src/utils/link/link-by-prompt.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const chalk = require('chalk')
44
const inquirer = require('inquirer')
55
const isEmpty = require('lodash/isEmpty')
66

7+
const { listSites } = require('../../lib/api')
78
const { getRepoData } = require('../get-repo-data')
89
const { track } = require('../telemetry')
910

@@ -46,7 +47,7 @@ module.exports = async function linkPrompts(context, flags = {}) {
4647
context.log()
4748
context.log(`Looking for sites connected to '${repoData.httpsUrl}'...`)
4849
context.log()
49-
const sites = await api.listSites({ filter: 'all' })
50+
const sites = await listSites({ api, options: { filter: 'all' } })
5051

5152
if (isEmpty(sites)) {
5253
context.error(
@@ -112,9 +113,9 @@ Run ${chalk.cyanBright('git remote -v')} to see a list of your git remotes.`)
112113

113114
let matchingSites
114115
try {
115-
matchingSites = await api.listSites({
116-
name: searchTerm,
117-
filter: 'all',
116+
matchingSites = await listSites({
117+
api,
118+
options: { name: searchTerm, filter: 'all' },
118119
})
119120
} catch (error) {
120121
if (error.status === 404) {
@@ -159,7 +160,7 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
159160

160161
let sites
161162
try {
162-
sites = await api.listSites({ filter: 'all' })
163+
sites = await listSites({ api, options: { maxPages: 1, filter: 'all' } })
163164
} catch (error) {
164165
context.error(error)
165166
}

0 commit comments

Comments
 (0)