diff --git a/packages/resource/src/index.js b/packages/resource/src/index.js index 29a29aa..baa7f65 100644 --- a/packages/resource/src/index.js +++ b/packages/resource/src/index.js @@ -1,6 +1,7 @@ import {gather} from '@latus/core'; export {default as Resource} from './resource'; +export * as JSONB from './jsonb'; export {default as JsonResource} from './json-resource'; export default { diff --git a/packages/resource/src/json-resource.js b/packages/resource/src/json-resource.js index 9b37b22..3df1b06 100644 --- a/packages/resource/src/json-resource.js +++ b/packages/resource/src/json-resource.js @@ -1,5 +1,6 @@ import merge from 'deepmerge'; +import {parse} from './jsonb'; import Resource from './resource'; export default class JsonResource extends Resource { @@ -48,19 +49,7 @@ export default class JsonResource extends Resource { } static async read(uri) { - const buffer = await super.read(uri); - let string; - if ('client' === process.env.SIDE) { - const decoder = new TextDecoder(); - string = decoder.decode(buffer); - } - else { - // eslint-disable-next-line global-require - const {StringDecoder} = require('string_decoder'); - const decoder = new StringDecoder(); - string = decoder.end(buffer); - } - return this.extendJson(JSON.parse(string)); + return this.extendJson(parse(await super.read(uri))); } } diff --git a/packages/resource/src/jsonb.js b/packages/resource/src/jsonb.js new file mode 100644 index 0000000..46f131b --- /dev/null +++ b/packages/resource/src/jsonb.js @@ -0,0 +1,16 @@ +export const bufferify = (value) => Buffer.from(JSON.stringify(value)); + +export const parse = (buffer) => { + let string; + if ('client' === process.env.SIDE) { + const decoder = new TextDecoder(); + string = decoder.decode(buffer); + } + else { + // eslint-disable-next-line global-require + const {StringDecoder} = require('string_decoder'); + const decoder = new StringDecoder(); + string = decoder.end(buffer); + } + return JSON.parse(string); +};