Merge branch 'react-pr' of https://github.com/netlify/netlify-cms into react-pr

This commit is contained in:
Cássio Zen
2016-09-05 12:13:51 -03:00
7 changed files with 450 additions and 11 deletions

View File

@ -35,9 +35,23 @@ export default class API {
});
}
urlFor(path, options) {
const params = [];
if (options.params) {
for (const key in options.params) {
params.push(`${key}=${encodeURIComponent(options.params[key])}`);
}
}
if (params.length) {
path += `?${params.join('&')}`;
}
return API_ROOT + path;
}
request(path, options = {}) {
const headers = this.requestHeaders(options.headers || {});
return fetch(API_ROOT + path, { ...options, headers: headers }).then((response) => {
const url = this.urlFor(path, options);
return fetch(url, { ...options, headers: headers }).then((response) => {
if (response.headers.get('Content-Type').match(/json/)) {
return this.parseJsonResponse(response);
}
@ -111,7 +125,7 @@ export default class API {
return this.request(`${this.repoURL}/contents/${path}`, {
headers: { Accept: 'application/vnd.github.VERSION.raw' },
body: { ref: this.branch },
params: { ref: this.branch },
cache: false
}).then((result) => {
if (sha) {
@ -125,7 +139,7 @@ export default class API {
listFiles(path) {
return this.request(`${this.repoURL}/contents/${path}`, {
body: { ref: this.branch }
params: { ref: this.branch }
});
}

View File

@ -1,7 +1,10 @@
import { createEntry } from '../../valueObjects/Entry';
import semaphore from 'semaphore';
import {createEntry} from '../../valueObjects/Entry';
import AuthenticationPage from './AuthenticationPage';
import API from './API';
const MAX_CONCURRENT_DOWNLOADS = 10;
export default class GitHub {
constructor(config) {
this.config = config;
@ -9,6 +12,7 @@ export default class GitHub {
throw 'The GitHub backend needs a "repo" in the backend configuration.';
}
this.repo = config.getIn(['backend', 'repo']);
this.branch = config.getIn(['backend', 'branch']) || 'master';
}
authComponent() {
@ -28,13 +32,22 @@ export default class GitHub {
}
entries(collection) {
return this.api.listFiles(collection.get('folder')).then((files) => (
Promise.all(files.map((file) => (
this.api.readFile(file.path, file.sha).then((data) => {
return createEntry(file.path, file.path.split('/').pop().replace(/\.[^\.]+$/, ''), data);
})
)))
)).then((entries) => ({
return this.api.listFiles(collection.get('folder')).then((files) => {
const sem = semaphore(MAX_CONCURRENT_DOWNLOADS);
const promises = [];
files.map((file) => {
promises.push(new Promise((resolve, reject) => {
return sem.take(() => this.api.readFile(file.path, file.sha).then((data) => {
resolve(createEntry(file.path, file.path.split('/').pop().replace(/\.[^\.]+$/, ''), data));
sem.leave();
}).catch((err) => {
sem.leave();
reject(err);
}));
}));
});
return Promise.all(promises);
}).then((entries) => ({
pagination: {},
entries
}));