All files api.item.js

90% Statements 27/30
16.67% Branches 1/6
88.89% Functions 8/9
90% Lines 27/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104        5x     5x   5x 2x   5x 1x   5x 1x   5x 4x 4x                           5x       1x   1x               5x         2x 2x   2x               5x         1x 1x   1x                 5x                           5x 4x   4x       4x  
// @flow
import got from 'got'
import { throws, asserts, joinParams, toBody } from './util'
 
const base_uri = `https://www.googleapis.com`
 
const insert_uri =
`${base_uri}/upload/chromewebstore/v1.1/items`
 
const update_uri = (id) =>
`${insert_uri}/${id}`
 
const publish_uri = (id) =>
`${check_uri(id)}/publish`
 
const check_uri = (id, projection) =>
`${base_uri}/chromewebstore/v1.1/items/${id}${joinParams({ projection })}`
 
const headers = (access_token) => {
  asserts(access_token, `[chenv] access_token is invalid`)
  return {
    'x-goog-api-version': '2',
    'Authorization': `Bearer ${access_token}`
  }
}
 
export type ItemResource = {
  kind: string,
  id: string,
  publicKey: string,
  uploadState: string,
  itemError: { error_detail: string }[]
}
 
export const insertItem = ({ token, body }: {
  token: string,
  body: ReadableStream
} = {}): Promise<ItemResource> => {
  asserts(body, `[chenv] body is required`)
 
  return got(insert_uri, {
    method: 'POST',
    headers: headers(token),
    body
  })
  .then(requestHandler)
}
 
export const updateItem = ({ token, id, body }: {
  token: string,
  id: string,
  body: ReadableStream
} = {}): Promise<ItemResource> => {
  asserts(id, `[chenv] id is ${id}`)
  asserts(body, `[chenv] body is required`)
 
  return got(update_uri(id), {
    method: 'PUT',
    headers: headers(token),
    body
  })
  .then(requestHandler)
}
 
export const publishItem = ({ token, id, target }: {
  token: string,
  id: string,
  target: string
} = {}): Promise<ItemResource> => {
  asserts(id, `[chenv] id is ${id}`)
  asserts(target, `[chenv] target is ${target}`)
 
  return got(publish_uri(id), {
    method: 'POST',
    headers: headers(token),
    json: true,
    body: { target }
  })
  .then(requestHandler)
}
 
export const checkItem = ({ token, id, projection }: {
  token: string,
  id: string,
  projection?: string
} = {}): Promise<ItemResource> => {
  asserts(id, `[chenv] id is ${id}`)
 
  return got(check_uri(id, projection), {
    method: 'GET',
    headers: headers(token)
  })
  .then(requestHandler)
}
 
const requestHandler = (res: { body: ItemResource | string }): ItemResource => {
  const body = toBody(res)
 
  Iif (body.uploadState !== 'SUCCESS') {
    throws(JSON.stringify(body))
  }
 
  return body
}