How to use cozy-client - 10 common examples

To help you get started, weā€™ve selected a few cozy-client examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cozy / cozy-bar / src / index.jsx View on Github external
// Force public mode in `/public` URLs
  if (!isPublic && /^\/public/.test(window.location.pathname)) {
    isPublic = true
  }

  if (!cozyClient) {
    const ccURI = cozyURL || getDefaultStackURL(isPublic)
    const ccToken = token || getDefaultToken(isPublic)
    const ccOptions = {
      uri: ccURI,
      token: ccToken
    }
    // eslint-disable-next-line no-console
    console.warn('Automatically made cozyClient. Options: ', ccOptions)
    const CozyClient = require('cozy-client').default
    cozyClient = new CozyClient({})
    // TODO, initializing CozyClient with a uri/token should automatically
    // call login(). Without login(), CozyClient.isLogged is false.
    cozyClient.login(ccOptions)
  }

  // store
  const getOrCreateStore = require('lib/store').default
  const reduxStore = getOrCreateStore()

  reduxStore.dispatch(setInfos(appName, appNamePrefix, appSlug))
  stack.init({
    cozyClient,
    onCreate: data => reduxStore.dispatch(onRealtimeCreate(data)),
    onDelete: data => reduxStore.dispatch(onRealtimeDelete(data))
  })
  if (lang) {
github cozy / create-cozy-app / packages / cozy-scripts / template / app / src / targets / browser / index.jsx View on Github external
const appNamePrefix = getDataOrDefault(
    data.cozyAppNamePrefix || require('../../../manifest.webapp').name_prefix,
    ''
  )

  const appName = getDataOrDefault(
    data.cozyAppName,
    require('../../../manifest.webapp').name
  )

  appLocale = getDataOrDefault(data.cozyLocale, 'en')

  const protocol = window.location ? window.location.protocol : 'https:'

  // initialize the client to interact with the cozy stack
  const client = new CozyClient({
    uri: `${protocol}//${data.cozyDomain}`,
    token: data.cozyToken,
    schema
  })

  // initialize the bar, common of all applications, it allows
  // platform features like apps navigation without doing anything
  cozy.bar.init({
    appName: appName,
    appNamePrefix: appNamePrefix,
    iconPath: appIcon,
    lang: appLocale,
    replaceTitleOnMobile: true
  })

  renderApp(client)
github cozy / cozy-bar / src / index.jsx View on Github external
const injectBarInDOM = data => {
  if (document.getElementById('coz-bar') !== null) {
    return
  }
  // import React related modules on init only
  const React = require('react')
  const { render } = require('react-dom')
  const { connect, Provider } = require('react-redux')
  const I18n = require('cozy-ui/react/I18n').default
  const Bar = require('components/Bar').default
  const CozyProvider = require('cozy-client').CozyProvider

  const { cozyClient } = data

  const barNode = createBarElement()
  const appNode = document.querySelector(APP_SELECTOR)
  if (!appNode) {
    // eslint-disable-next-line no-console
    console.warn(
      `Cozy-bar is looking for a "${APP_SELECTOR}" tag that contains your application and can't find it :'(ā€¦ The BAR is now disabled`
    )
    return null
  }

  document.body.insertBefore(barNode, appNode)

  // method to put cozy-bar z-index on the top when Drawer visible and vice versa
github cozy / cozy.github.io / en / cozy-banks / src / ducks / client / mobile / mobile.js View on Github external
'https://downcloud.cozycloud.cc/upload/cozy-banks/email-assets/logo-bank.png',
      notificationPlatform: 'firebase'
    },
    links: getLinks({
      pouchLink: {
        // TODO: the revocation check via cozy-pouch-link should not be
        // done in the app. We should find a way to have this in a shared
        // repository, possibly in cozy-client or cozy-pouch-link
        onSyncError: async () => {
          checkForRevocation(client)
        }
      }
    })
  }

  client = new CozyClient(merge(manifestOptions, banksOptions))
  registerPluginsAndHandlers(client)
  return client
}
github cozy / cozy-bar / examples / index.jsx View on Github external
)
})

const appInfo = {
  name: 'Example bar',
  slug: 'example-bar',
  softwareID: 'io.cozy.example',
  redirectURI: 'http://localhost:1234/auth',
  protocol: 'cozyexample://',
  universalLinkDomain: 'https://links.cozyexample.com'
}

const client = new CozyClient({
  scope: ['io.cozy.apps', 'io.cozy.konnectors'],
  // TODO client: All the oauth information could come from the manifest
  oauth: {
    clientName: appInfo.name,
    softwareID: appInfo.softwareID,
    redirectURI: appInfo.redirectURI
  }
})

// TODO bar: should only need a correctly configured client
client.on('login', () => {
  const oldOptions = {
    token: client.stackClient.token.accessToken,
    cozyURL: client.stackClient.uri
  }
  const newOptions = {
github cozy / cozy.github.io / en / cozy-banks / src / ducks / reimbursements / Reimbursements.jsx View on Github external
)
  }
}

function mapStateToProps(state) {
  return {
    groupedExpenses: getGroupedFilteredExpenses(state),
    currentPeriod: getPeriod(state),
    filteringDoc: getFilteringDoc(state)
  }
}

const Reimbursements = compose(
  translate(),
  queryConnect({
    transactions: transactionsConn
  }),
  connect(mapStateToProps),
  withFilters,
  // We need to have a different query name otherwise we end with an infinite
  // loading
  withBrands({ queryName: 'reimbursementsPageTriggers' })
)(DumbReimbursements)

export default Reimbursements
github cozy / cozy.github.io / en / cozy-banks / src / ducks / settings / AccountsSettings.jsx View on Github external
)
  }
}

// TODO reactivate when we understand how sharings work
// const fetchAccountsSharingInfo = props => {
//   return Promise.resolve([])
// const { accounts } = props
// with cozy-client
// return Promise.all(accounts.data.map(account => {
//   return props.dispatch(fetchSharingInfo(ACCOUNT_DOCTYPE, account._id))
// }))
// }

export default compose(
  queryConnect({
    accountsCollection: accountsConn,
    apps: { query: () => Q(APP_DOCTYPE), as: 'apps' }
  }),
  translate()
)(AccountsSettings)
github cozy / cozy.github.io / en / cozy-banks / src / ducks / transactions / TransactionPageErrors / TransactionPageErrors.jsx View on Github external
// Use static property to be able to override
TransactionPageErrors.errorTypeToComponent = {
  'errored-trigger': TriggerErrorCard
}

TransactionPageErrors.propTypes = {
  triggerCol: PropTypes.object.isRequired,
  accounts: PropTypes.array.isRequired
}

export default compose(
  connect(state => ({
    accounts: getFilteredAccounts(state)
  })),
  queryConnect({
    triggerCol: {
      ...triggersConn,
      fetchPolicy: CozyClient.fetchPolicies.noFetch
    }
  }),
  React.memo
)(TransactionPageErrors)
github cozy / cozy-ui / react / ContactsListModal / index.jsx View on Github external
import Modal, { ModalHeader, ModalDescription } from '../Modal'
import Spinner from '../Spinner'
import styles from './styles.styl'
import Input from '../Input'
import PropTypes from 'prop-types'
import withBreakpoints from '../helpers/withBreakpoints'
import Button from '../Button'
import { Contact } from 'cozy-doctypes'
import AddContactButton from './AddContactButton'
import EmptyMessage from './EmptyMessage'

const thirtySeconds = 30000
// the check in the next line is because it otherwise requires cozy-client
// even for libs or apps that do not use cozy-client nor ContactsListModal
const olderThan30s = fetchPolicies
  ? fetchPolicies.olderThan(thirtySeconds)
  : () => true

const mkFilter = filterStr => contacts => {
  if (!filterStr) {
    return contacts
  }

  const f = filterStr.toLowerCase()

  // TODO better filtering methods can be extracted from drive. See https://github.com/cozy/cozy-ui/pull/1273#discussion_r351845385
  return contacts.filter(contact => {
    const displayName = Contact.getDisplayName(contact)

    if (!displayName) {
      return false
    }
github cozy / cozy-bar / test / lib / stack-client / stack-client.intents.spec.js View on Github external
describe('intents', () => {
    const stackClient = {
      token: { token: 'mytoken' },
      uri: 'https://test.mycozy.cloud'
    }

    const cozyClient = new CozyClient({
      stackClient
    })

    const params = {
      cozyClient,
      onCreateApp: function() {},
      onDeleteApp: function() {}
    }

    beforeAll(async () => {
      await stack.init(params)
    })

    it('should return correctly the Intents instance', () => {
      const intents = stack.get.intents()
      expect(intents).toBeInstanceOf(Intents)