Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

feat: add representation return option to delete() #207

Merged
merged 1 commit into from
Dec 14, 2021
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
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,25 @@ Promise<void>

#### delete()

| Name | Type | Default | Examples |
| ---- | -------- | ------------ | ---------- |
| path | `string` | **required** | `"/books"` |
| Name | Type | Default | Examples |
| --------- | ------------------------------------------------- | ------------ | ---------- |
| path | `string` | **required** | `"/books"` |
| `options` | `{ return?: 'headers-only' \| 'representation' }` | | |

Return value:

```ts
Promise<void>
```

or (with `{ return: "representation" }`):

```ts
Promise<{
data: T[]
}>
```

## Contribute

Please check our [contributing documentation][lnk-contributing].
Expand Down
41 changes: 41 additions & 0 deletions e2e/delete.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const assert = require('assert')

const { create } = require('..')
const handleError = require('./helpers/handleError')

const postgrestClient = create({
axiosConfig: { baseURL: 'http://localhost:3000' },
})

describe('E2E: #delete()', () => {
it(`should return undefined with a single item and no option`, async () => {
try {
await postgrestClient.post('/customers', {
email: 'bob.marley@protonmail.com',
name: 'Bob Marley',
})
const result = await postgrestClient.eq('email', 'bob.marley@protonmail.com').delete('/customers')

assert.equal(result.data, undefined)
} catch (err) {
handleError(err)
}
})

it(`should return undefined with a single item and no option`, async () => {
try {
await postgrestClient.post('/customers', {
email: 'bob.marley@protonmail.com',
name: 'Bob Marley',
})
const result = await postgrestClient.eq('email', 'bob.marley@protonmail.com').delete('/customers', {
return: 'representation',
})

assert.equal(result.data.email, 'bob.marley@protonmail.com')
assert.equal(result.data.name, 'Bob Marley')
} catch (err) {
handleError(err)
}
})
})
2 changes: 1 addition & 1 deletion e2e/ilike.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const postgrestClient = create({
axiosConfig: { baseURL: 'http://localhost:3000' },
})

describe('E2E: ILIKE', () => {
describe('E2E: #ilike()', () => {
it(`should find all the books which title contains "an" or "An"`, async () => {
const { data: result } = await postgrestClient
.select('title')
Expand Down
2 changes: 1 addition & 1 deletion e2e/like.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const postgrestClient = create({
axiosConfig: { baseURL: 'http://localhost:3000' },
})

describe('E2E: LIKE', () => {
describe('E2E: #like()', () => {
it(`should find all the books which title contains "u"`, async () => {
const { data: result } = await postgrestClient
.select('title')
Expand Down
2 changes: 1 addition & 1 deletion e2e/post.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const postgrestClient = create({
axiosConfig: { baseURL: 'http://localhost:3000' },
})

describe('E2E: POST', () => {
describe('E2E: #post()', () => {
beforeEach(async () => {
await postgrestClient.gt('id', 1).delete('/customers')
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test:e2e:v8": "cross-env POSTGREST_VERSION=v8.0.0.20211102 yarn setup && yarn test:e2e",
"test:e2e:v9": "yarn setup && yarn test:e2e",
"test:lint": "eslint .",
"test:unit": "jest ./src/**/*.test.ts",
"test:unit": "jest",
"test:watch": "yarn test:unit --watch"
},
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion src/Postgrester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ describe('Postgrester', () => {
test('should call axios.delete() with the expected params', async () => {
await postgresterClient.delete('/path')

expect(mockedAxios.delete).toHaveBeenCalledWith('/path')
expect(mockedAxios.delete).toHaveBeenCalledWith('/path', {
headers: undefined,
})
})
})
})
11 changes: 9 additions & 2 deletions src/Postgrester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,18 @@ const Postgrester: PostgresterConstructor = class Postgrester implements Postgre
await this.axios.put(uri, data)
}

public async delete(path: string) {
public async delete(path: string, options: any = {}): Promise<any> {
const uri = this.buildUri(path)
this.reset()

await this.axios.delete(uri)
const headers = this.buildHeaders({ preferOptions: options })
const response = await this.axios.delete(uri, {
headers,
})

return {
data: response?.data.length ? response.data[0] : undefined,
}
}

public select(selector: string) {
Expand Down
25 changes: 24 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export interface PostgresterConstructor {

export interface PostgresterInstance {
and: PostgresterInstance
delete(path: string): Promise<void>
eq(column: string, value: boolean | number | string | null, withQuotes?: boolean): PostgresterInstance
get<T = Data>(
path: string,
Expand Down Expand Up @@ -73,6 +72,12 @@ export interface PostgresterInstance {
): Promise<{
data: T
}>
post<T extends Data = any>(
path: string,
data: Partial<T>[],
): Promise<{
data: undefined
}>
post<T extends Data = any>(
path: string,
data: Partial<T>[],
Expand All @@ -89,6 +94,24 @@ export interface PostgresterInstance {
): Promise<{
data: undefined
}>

delete(path: string): Promise<void>
delete<T extends Data = any>(
path: string,
options: {
return: 'representation'
},
): Promise<{
data: T[]
}>
delete(
path: string,
options: {
return: 'headers-only'
},
): Promise<{
data: undefined
}>
}

export interface PostgresterStatic extends PostgresterInstance {
Expand Down