本文整理汇总了TypeScript中isomorphic-fetch类的典型用法代码示例。如果您正苦于以下问题:TypeScript isomorphic-fetch类的具体用法?TypeScript isomorphic-fetch怎么用?TypeScript isomorphic-fetch使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了isomorphic-fetch类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test_whatwgTestCases_es6
function test_whatwgTestCases_es6() {
var headers = new Headers();
headers.append("Content-Type", "application/json");
var requestOptions: RequestInit = {
method: "POST",
headers: headers,
mode: 'same-origin',
credentials: 'omit',
cache: 'default'
};
expectSuccess(fetchImportedViaES6Module('http://localhost:3000/poster', requestOptions), 'Post response:');
var requestOptions: RequestInit = {
method: "POST",
headers: {
'Content-Type': 'application/json'
}
};
expectSuccess(fetchImportedViaES6Module('http://localhost:3000/poster', requestOptions), 'Post response:');
var requestOptions: RequestInit = {
method: "POST",
headers: {
'Content-Type': 'application/json'
}
};
var request: Request = new Request('http://localhost:3000/poster', requestOptions);
expectSuccess(fetchImportedViaES6Module(request), 'Post response:');
}
示例2: test_isomorphicFetchTestCases_es6
function test_isomorphicFetchTestCases_es6() {
expectSuccess(fetchImportedViaES6Module('http://localhost:3000/good'), 'Good response');
fetchImportedViaES6Module('http://localhost:3000/bad')
.then((response: IResponse) => {
return response.text();
})
.catch((err) => {
});
}
示例3: return
return (dispatch: Dispatch<any>) => {
fetch('//localhost:1337/user/'+ user.id +'/services', {
method: 'POST',
credentials: 'include',
body: JSON.stringify({
title: title,
description: description,
price: price
})
})
.then(function(response: any) {
if (response.status >= 400) {
dispatch(throwErrror(__('Bad response from server.')))
}
return response.json();
})
.then(function(response: any) {
if(response.services)
{
dispatch(addedService(response.services))
dispatch(push('/'))
}
})
.catch(function(err: any) {
dispatch(throwErrror(__('Error : cannot connect to the server.')))
})
}
示例4:
{ _:({postsBySubreddit,selectedSubreddit},payload,meta,actions,dispatch)=>
shouldFetchPosts(postsBySubreddit,selectedSubreddit) ?
fetch(`https://www.reddit.com/r/${selectedSubreddit}.json`)
.then(response => response.json())
.then(json=>json.data.children.map(child=>child.data))
.catch(err=>console.log(err)):
null
示例5: async
const uploadToServer = async (canvas: HTMLCanvasElement) => {
const data = canvas.toDataURL('image/jpeg', 0.6);
const encodedjpg = data.substring(data.indexOf(',') + 1, data.length);
const body = new FormData();
body.append('filename', 'result_');
body.append('image', encodedjpg);
const res = await fetch('http://thesecretfarm.com/cheerup/writeImage.php', {
method: 'POST',
body,
});
const resJson = await res.json();
const md = new MobileDetect(window.navigator.userAgent);
if (md.mobile()) {
const shareURL = `https://www.facebook.com/dialog/feed?app_id=${Setting.fbAppId}&link=${Setting.shareInfo.link}&name=${Setting.shareInfo.title}&caption=${Setting.shareInfo.caption}&description=${Setting.shareInfo.description}&picture=${'http://www.thesecretfarm.com/cheerup/result/' + resJson.filename + '.jpg'}&redirect_uri=${Setting.redirectURL}`;
window.location.href = shareURL;
} else {
const uiParams = {
method: 'feed',
name: Setting.shareInfo.title,
title: Setting.shareInfo.title,
link: Setting.shareInfo.link,
caption: Setting.shareInfo.caption,
description: Setting.shareInfo.description,
display: 'iframe',
picture: 'http://www.thesecretfarm.com/cheerup/result/' + resJson.filename + '.jpg', // 'http://www.thesecretfarm.com/creativegift/star_55652e81082027.jpg'//'http://gth.co.th/startheque/result/'+res.filename+'.jpg',//'http://www.thesecretfarm.com/creativegift/star_55652e81082027.jpg'//
} as any;
return new Promise<any>((fulfill, reject) => {
FB.ui(uiParams, function (response: any): void {
fulfill(response);
});
});
}
};
示例6: Promise
return new Promise((resolve, reject) => {
console.log(url);
fetch(url)
.then(apiResult => apiResult.json())
.then(json => resolve(json)) // Success
.catch(error => reject(error)); // Fail
});
示例7: fetch
export function fetchData<T>(url: string, level?: FetchLevel): Promise<T> {
const fullURL = Constants.API_SERVER_BASE_URL + url;
if (level && appState.isClientEnv) {
appState.addFetch(url, level);
}
return fetch(fullURL,
{
headers: {
"Accept": "application/vnd.api+json"
}
})
.then(response => response.json())
.then((data) => {
if (level && appState.isClientEnv) {
appState.deleteFetch(url);
}
return new Deserializer({ keyForAttribute: "camelCase" }).deserialize(data);
})
.catch((err: Error) => {
console.error(err.message);
return err;
});
};
示例8: login
login(email?: string, password?: string): Promise<{ readonly token: string }> {
if (!this.host) throw new Error('Should set the host url')
const options = {
method: Method.POST,
headers: new Headers({
'Content-Type': 'application/json',
}),
body: JSON.stringify({
email: email || this.email,
password: password || this.password,
}),
}
const request = fetch(`${this.host}${Path.LOGIN}`, options)
return Promise.race([request, this.timeoutPromise()])
.then(async (value: any) => {
if (value.ok) return await value.json()
throw await value.text()
})
.catch(e => {
throw e
})
}
示例9: return
return (dispatch: Dispatch<any>) => {
dispatch(loginRequest())
fetch('//localhost:1337/login', {
method: 'POST',
credentials: 'include',
body: JSON.stringify({
email: username,
password: password,
})
})
.then(function(response: any) {
if (response.status >= 400) {
dispatch(throwErrror(__('Bad response from server.')))
}
return response.json();
})
.then(function(response: any) {
if(response.user)
{
let user = response.user
dispatch(initCurrentUser(user))
dispatch(loggedInSuccess())
dispatch(push('/'))
} else {
dispatch(loggedInFailed(response.message))
}
})
.catch(function(err: any) {
dispatch(throwErrror(__('Error : cannot connect to the server.')))
})
}
示例10: it
it("index.html returns null", (done: MochaDone) => {
let p2: Promise<ParsedTimestamp> =
fetch(serverUrl + "/index.html")
.then((res: IResponse) => res.json());
promiseAssert(p2, done, (j: ParsedTimestamp): void => {
chai.expect(j).to.deep.equal(nullTS);
});
});