Skip to content

Instantly share code, notes, and snippets.

@alobato
Last active February 25, 2019 17:20
Show Gist options
  • Save alobato/de3c8517785a7b43319e5a45c9e992a7 to your computer and use it in GitHub Desktop.
Save alobato/de3c8517785a7b43319e5a45c9e992a7 to your computer and use it in GitHub Desktop.
// https://github.com/drcallaway/apollo-link-timeout/
import { ApolloLink, Observable } from 'apollo-link'
const DEFAULT_TIMEOUT = 15000
class ApolloLinkTimeout extends ApolloLink {
constructor(timeout) {
super()
this.timeout = timeout || DEFAULT_TIMEOUT
}
request(operation, forward) {
let controller
let ctxTimeout = operation.getContext().timeout || null
if (ctxTimeout <= 0) ctxTimeout = null
if (typeof AbortController !== 'undefined') {
const context = operation.getContext()
let fetchOptions = context.fetchOptions || {}
controller = fetchOptions.controller || new AbortController()
fetchOptions = { ...fetchOptions, controller, signal: controller.signal }
operation.setContext({ fetchOptions })
}
const chainObservable = forward(operation)
const operationType = (operation.query.definitions).find(def => def.kind === 'OperationDefinition').operation
if (this.timeout <= 0 || operationType === 'subscription') return chainObservable
const localObservable = new Observable(observer => {
let timer
const subscription = chainObservable.subscribe(
result => {
clearTimeout(timer)
observer.next(result)
observer.complete()
},
error => {
clearTimeout(timer)
observer.error(error)
observer.complete()
}
)
timer = setTimeout(() => {
if (controller) controller.abort()
observer.error(new Error('Timeout exceeded'))
subscription.unsubscribe()
}, ctxTimeout || this.timeout)
return () => {
clearTimeout(timer)
subscription.unsubscribe()
}
})
return localObservable
}
}
export default ApolloLinkTimeout
export const weekdays = 'Domingo_Segunda-feira_Terça-feira_Quarta-feira_Quinta-feira_Sexta-feira_Sábado'.split('_')
export const months = 'Janeiro_Fevereiro_Março_Abril_Maio_Junho_Julho_Agosto_Setembro_Outubro_Novembro_Dezembro'.split('_')
export const today = () => {
const today = new Date()
today.setHours(0, 0, 0, 0)
return today
}
export const yesterday = () => {
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
yesterday.setHours(0, 0, 0, 0)
return yesterday
}
export const lastWeek = () => {
const lastWeek = new Date()
lastWeek.setDate(lastWeek.getDate() - 7)
lastWeek.setHours(0, 0, 0, 0)
return lastWeek
}
export const beginningOfThisMonth = () => {
const thisMonth = new Date()
thisMonth.setDate(1)
thisMonth.setHours(0, 0, 0, 0)
return thisMonth
}
export const beginningOfPastMonth = (monthAgo = 1) => {
const lastMonth = new Date()
lastMonth.setMonth(lastMonth.getMonth() - monthAgo)
lastMonth.setDate(1)
lastMonth.setHours(0, 0, 0, 0)
return lastMonth
}
export const padStart = (string, length, pad) => {
const s = String(string)
if (!s || s.length >= length) return string
return `${Array((length + 1) - s.length).join(pad)}${string}`
}
export const arrayDateTimeFromIsoString = text => text.slice(0, 16).replace('T', '-').replace(/:/g, '-').split('-')
export const formatDateTimeFromIsoString = text => {
const d = arrayDateTimeFromIsoString(text)
return `${d[2]}/${d[1]}/${d[0]} ${d[3]}:${d[4]}`
}
export const formatDateFromIsoString = text => {
const d = arrayDateTimeFromIsoString(text)
return `${d[2]}/${d[1]}/${d[0]}`
}
export const formatTimeFromIsoString = text => {
const d = arrayDateTimeFromIsoString(text)
return `${d[3]}:${d[4]}`
}
export const percent = (value, total) => Math.round((value * 100) / total)
export const w = text => text.split(' ')
import * as Yup from 'yup'
Yup.setLocale({
mixed: {
default: 'Campo inválido',
required: 'Campo obrigatório'
},
string: {
min: params => `Deve ter pelo menos ${params.min} caracteres`
}
})
const CpfIsValid = value => {
if (!value) value = ''
value = value.replace(/\D/g, '')
let soma = 0
if (value === '00000000000') return false
if (value === '11111111111') return false
if (value === '22222222222') return false
if (value === '33333333333') return false
if (value === '44444444444') return false
if (value === '55555555555') return false
if (value === '66666666666') return false
if (value === '77777777777') return false
if (value === '88888888888') return false
if (value === '99999999999') return false
for(let i=1; i<=9; i++) soma = soma + parseInt(value.substring(i-1, i), 10) * (11 - i)
let resto = (soma * 10) % 11
if ((resto === 10) || (resto === 11)) resto = 0
if (resto !== parseInt(value.substring(9, 10), 10)) return false
soma = 0
for(let i = 1; i <= 10; i++) soma = soma + parseInt(value.substring(i-1, i), 10)*(12-i)
resto = (soma * 10) % 11
if ((resto === 10) || (resto === 11)) resto = 0
if (resto !== parseInt(value.substring(10, 11), 10)) return false
return true
}
Yup.addMethod(Yup.string, 'cpf', function (message) {
return this
.test({
name: 'cpf',
exclusive: true,
message: message || '',
test: value => {
if (!value) return true
return CpfIsValid(value)
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment