Skip to content

Instantly share code, notes, and snippets.

Nikolay mkozhukharenko

Block or report user

Report or block mkozhukharenko

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mkozhukharenko
mkozhukharenko / api.js
Created Jan 14, 2018
Axios with refresh handlig
View api.js
import axios from 'axios';
import { IUser } from '../types/index';
let isRefreshing = false;
let refreshSubscribers: any = [];
export const API_VERSION = 'v1';
export const BACKEND_API_URL = process.env.NODE_ENV === 'development'
? '//127.0.0.1:3000'
: process.env.REACT_APP_BACKEND_API_URL || `${window.location.protocol}//${window.location.hostname}`;
View progress.story.tsx
import * as React from 'react'
import { storiesOf } from '@storybook/react'
import Progress from './progress'
storiesOf('Progress', module)
.add('Simple', () => (
<div>
<div style={{width: 250}}>
<Progress percent={20} type="line"/>
</div>
@mkozhukharenko
mkozhukharenko / wait-for.ts
Created Sep 11, 2017
Run function only when the condition is met
View wait-for.ts
interface IWaitForArgs {
test: () => boolean // run function only if test() returns true
interval: number
count: number // should be 0, required
maxAttempts: number // max number of attempts
run: () => any // function to run
}
function waitFor({test, interval, count, maxAttempts, run}: IWaitForArgs) {
// try to run function only maxAttempts times
@mkozhukharenko
mkozhukharenko / bing-api.js
Created Jul 26, 2017
Bing API search javascript
View bing-api.js
let request = async (query: string): Promise<any[]> => {
const myHeaders = new Headers()
myHeaders.append('Ocp-Apim-Subscription-Key', 'b5deb0d4be5a4dcaaa478f7b343baa3d')
const params = {
q: query,
safeSearch: 'Strict',
license: 'Public'
}
const urlParams = new URLSearchParams((Object as any).entries(params)) // entries works in Chrome
const url = 'https://api.cognitive.microsoft.com/bing/v5.0/images/search?' + urlParams.toString()
View compose-middlewares.js
let compose = (middlewares) => {
return function () {
return dispatch(0)
function dispatch(i) {
let fn = middlewares[i]
if (!fn) {
return
}
fn(function next() {
return dispatch(i + 1)
@mkozhukharenko
mkozhukharenko / express-auth-promises2.js
Last active Apr 11, 2017
Expressks auth with promise .js
View express-auth-promises2.js
app.post('/', function (req, res, next) {
var body = req.body;
if (!body.name || !body.email || !body.password) {
return res.status(400).send("Missing username or email or password")
};
// case #1- if user was registered (socials platform) before -> just add a password;
User.findOneAndUpdate({
email: body.email
}, {
$set: { password: body.password }
@mkozhukharenko
mkozhukharenko / basic-express.js
Created Apr 8, 2017
Real compose function implementation (composing middlewares)
View basic-express.js
var app = {
middlewares: [],
use(fn) {
this.middlewares.push(fn)
},
compose(middleware) {
return function (next) {
return dispatch(0)
function dispatch(i) {
let fn = middleware[i]
View login-example-co.js
var co = require('co')
app.post('/',function (req, res, next) {
var body = req.body;
if (!body.name || !body.email || !body.password) {
return res.status(400).send("Missing a username or email or password")
};
co(function *(){
View callback-missed-error.js
User.findOneAndUpdate({}, (err, user) => {
if (err) { // !!!!!
return res.status(500).send(err)
}
// everething is ok, do smth
});
View callback.js
app.post('/',function (req, res, next) {
var body = req.body;
if (!body.name || !body.email || !body.password) {
return res.status(400).send("Missing username or email or password")
};
// case #1- if user was registered (socials platform) before -> just add a password;
User.findOneAndUpdate({
email: body.email
}, {
$set: {password: body.password}
You can’t perform that action at this time.