Skip to content

Instantly share code, notes, and snippets.

@Calvin-Huang
Calvin-Huang / axios-validatestatus.js
Created September 16, 2022 04:41
simple example to show how to not throw the error when expected status code
const {
status,
} = await axios.get(`/foo/${id}`, {
validateStatus: status => status === 200 || status === 404,
})
@Calvin-Huang
Calvin-Huang / axios-interceptor-1.js
Created September 16, 2022 04:35
simple example to transform response from underscore to camelCase in axios interceptor
// Add a response interceptor
axios.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
return {
...response,
data: humps.camelizeKeys(response.data),
}
})
const axios = require('axios');
async function getCycPool(address) {
const obj = {};
const resultList = await Promise.allSettled([
await axios.get('https://api.bscscan.com/api?module=proxy&action=eth_call&to=0x9634cfd96f1499990695ebbc081b4ee8d63d2e12&data=0902f1ac&tag=latest'),
await axios.get('https://api.bscscan.com/api?module=proxy&action=eth_call&to=0x9634cfd96f1499990695ebbc081b4ee8d63d2e12&data=18160ddd&tag=latest'),
await axios.get(`https://api.bscscan.com/api?module=proxy&action=eth_call&to=0x567da514637cfd7f9e1f185ae4aa163b3ebb5363&data=0xf40f0f52000000000000000000000000${address.replace(/^0x/, '')}&tag=latest`),
await axios.get(` https://api.bscscan.com/api?module=proxy&action=eth_call&to=0x567da514637cfd7f9e1f185ae4aa163b3ebb5363&data=0x1959a002000000000000000000000000${address.replace(/^0x/, '')}&tag=latest`),
await axios.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=cyclone-protocol'),
@Calvin-Huang
Calvin-Huang / nextjs_SSR_sample_home_page.jsx
Created October 2, 2020 03:15
The sample which works fine without any shared state problems.
/* /page/home.jsx */
import { getAxios } from '@util/axios'
const Home = ({ user }) => {
return (
<h1>{user.name}</h1>
)
}
Home.getServerSideProps = async ({ req }) => {
@Calvin-Huang
Calvin-Huang / nextjs_SSR_bug_sample_home_page.jsx
Last active October 2, 2020 03:17
Get user's credential before rendering to prevent showing empty username.
/* /page/home.jsx */
import axios from '@util/axios'
const Home = ({ user }) => {
return (
<h1>{user.name}</h1>
)
}
Home.getServerSideProps = async ({ req }) => {
@Calvin-Huang
Calvin-Huang / nextjs_SPA_sample_home_page.jsx
Last active October 1, 2020 12:44
The most simple and common implementation for login and request user's data with credential.
/* /page/home.jsx */
import { useState, useEffect } from 'react'
import axios from '@util/axios'
const Home = () => {
const [user, setUser] = useState({})
useEffect(async () => {
const { data } = await axios.get('https://mock.bugfree.app/api/me')
setUser(data)
})
@interface VIGP2API()
{
AnGooGeneralCallbackBlock _callBack;
}
@property AnGooGeneralCallbackBlock PushMsgCallbackBlock;
@end
static TIAESCipher *_sgAesEngineObj = nil;
@implementation VIGP2API
@interface LoginVC () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
{
UserController *_uco;
RegisterNC *_rNC;
MBProgressHUD *_hud;
LoginUserIdTVC* _tvcUserId;
LoginPasswdTVC* _tvcPasswd;
NS_ASSUME_NONNULL_BEGIN
@interface UserController : NSObject
+(UserController*)sigleton;
//Login
- (void) userLoginWithAccountId:(NSString*)accId
passwd:(NSString*)passwd
socialType:(NSString*)socialType //N:None; FB:facebook; G:GSI
completion:(GeneralCallbackBlock)completion;
@interface UserController()
{
dispatch_semaphore_t _sema_use_log;
}
@end
static UserController *_sguco = nil;
@implementation UserController