Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/grabkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
"description": "TypeScript HTTP client for explicit METHOD /path calls. JSON:API by default.",
"main": "build/cjs/grabkit.js",
"types": "build/types/grabkit.d.ts",
"module": "build/esm/grabkit.js",
"module": "build/esm/grabkit.mjs",
"exports": {
".": {
"types": "./build/types/grabkit.d.ts",
"import": "./build/esm/grabkit.mjs",
"require": "./build/cjs/grabkit.js"
},
"./package.json": "./package.json"
},
"repository": "https://github.com/mrlemoos/grabkit",
"author": "Leonardo Lemos <leo.lemos.ds@icloud.com>",
"license": "Apache-2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/grabkit/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const require = createRequire(import.meta.url);
const tslibPath = require.resolve('tslib');

const projectRoot = 'packages/grabkit';
const ESM_FILENAME = `${projectRoot}/build/esm/grabkit.js`;
const ESM_FILENAME = `${projectRoot}/build/esm/grabkit.mjs`;
const CJS_FILENAME = `${projectRoot}/build/cjs/grabkit.js`;
const TYPES_FILENAME = `${projectRoot}/build/types/grabkit.d.ts`;

Expand Down
4 changes: 4 additions & 0 deletions packages/grabkit/src/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class Call {
const text = await response.text();

if (text.length === 0) {
// An empty 2xx (e.g. `202 Accepted` with no body) is success, like 204.
if (response.ok) {
return { ok: true, status: response.status, parsed: null, empty: true };
}
return {
ok: false,
error: new GrabkitTransportError('Grabkit: empty response body'),
Expand Down
21 changes: 21 additions & 0 deletions packages/grabkit/src/grabkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ describe('grabkit', () => {
expect(meta).toEqual({ statusCode: 204 });
});

it.each([200, 201, 202])('treats an empty %i body as success with null data', async (status) => {
vi.stubGlobal('fetch', vi.fn(async () => new Response(null, { status })));
const grab = grabkit('https://api.example.com', { format: 'json' });

const [data, error, meta] = await grab('POST /events', { body: { name: 'signup' } });

expect(error).toBeNull();
expect(data).toBeNull();
expect(meta).toEqual({ statusCode: status });
});

it('still reports an empty non-2xx body as a failure', async () => {
vi.stubGlobal('fetch', vi.fn(async () => new Response(null, { status: 500 })));
const grab = grabkit('https://api.example.com', { format: 'json' });

const [data, error] = await grab('POST /events', { body: { name: 'signup' } });

expect(data).toBeNull();
expect(error).not.toBeNull();
});

it('serialises JSON:API write bodies and sets media type headers by default', async () => {
const fetchMock = vi.fn(async () => new Response(null, { status: 204 }));
vi.stubGlobal('fetch', fetchMock);
Expand Down
12 changes: 12 additions & 0 deletions packages/grabkit/src/publishBundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ describe('publish bundle', () => {
it('includes LICENSE in the npm tarball', () => {
expect(listPackFiles()).toContain('LICENSE');
});

it.each([
{ runtime: 'Node ESM', args: ['--input-type=module', '-e', "const m = await import('grabkit'); console.log(typeof m.default)"] },
{ runtime: 'Node CJS', args: ['-e', "console.log(typeof require('grabkit').default)"] },
])('exposes a callable default export to $runtime consumers', ({ args }) => {
// Self-reference by package name resolves through `exports`, like a real install.
const { stdout, stderr, status } = spawnSync(process.execPath, args, { cwd: packageRoot, encoding: 'utf8' });

expect(stderr).toBe('');
expect(status).toBe(0);
expect(stdout.trim()).toBe('function');
});
});
Loading