Skip to content
This repository was archived by the owner on Mar 19, 2026. It is now read-only.

Commit 3248e27

Browse files
AVaksmanBenjamin E. Coesofisl
authored
feat: add ignoreNotFound to service-object#delete (#634)
* feat: add ignoreNotFound to service-object#delete * chore: lint Co-authored-by: Benjamin E. Coe <[email protected]> Co-authored-by: sofisl <[email protected]>
1 parent 7f85803 commit 3248e27

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

src/service-object.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export interface CreateCallback<T> {
115115
(err: ApiError | null, instance?: T | null, ...args: any[]): void;
116116
}
117117

118-
export type DeleteOptions = object;
118+
export type DeleteOptions = {ignoreNotFound?: boolean} & object;
119119
export interface DeleteCallback {
120120
(err: Error | null, apiResponse?: r.Response): void;
121121
}
@@ -270,14 +270,17 @@ class ServiceObject<T = any> extends EventEmitter {
270270
delete(options: DeleteOptions, callback: DeleteCallback): void;
271271
delete(callback: DeleteCallback): void;
272272
delete(
273-
optionsOrCallback: DeleteOptions | DeleteCallback,
273+
optionsOrCallback?: DeleteOptions | DeleteCallback,
274274
cb?: DeleteCallback
275275
): Promise<[r.Response]> | void {
276276
const [options, callback] = util.maybeOptionsOrCallback<
277277
DeleteOptions,
278278
DeleteCallback
279279
>(optionsOrCallback, cb);
280280

281+
const ignoreNotFound = options.ignoreNotFound!;
282+
delete options.ignoreNotFound;
283+
281284
const methodConfig =
282285
(typeof this.methods.delete === 'object' && this.methods.delete) || {};
283286

@@ -295,7 +298,18 @@ class ServiceObject<T = any> extends EventEmitter {
295298

296299
// The `request` method may have been overridden to hold any special
297300
// behavior. Ensure we call the original `request` method.
298-
ServiceObject.prototype.request.call(this, reqOpts, callback);
301+
ServiceObject.prototype.request.call(
302+
this,
303+
reqOpts,
304+
(err: ApiError | null, ...args) => {
305+
if (err) {
306+
if (err.code === 404 && ignoreNotFound) {
307+
err = null;
308+
}
309+
}
310+
callback(err, ...(args as any));
311+
}
312+
);
299313
}
300314

301315
/**

test/service-object.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,40 @@ describe('ServiceObject', () => {
318318
serviceObject.delete();
319319
});
320320

321+
it('should respect ignoreNotFound opion', done => {
322+
const options = {ignoreNotFound: true};
323+
const error = new ApiError({code: 404, response: {} as r.Response});
324+
sandbox.stub(ServiceObject.prototype, 'request').callsArgWith(1, error);
325+
serviceObject.delete(options, (err, apiResponse_) => {
326+
assert.ifError(err);
327+
assert.strictEqual(apiResponse_, undefined);
328+
done();
329+
});
330+
});
331+
332+
it('should propagate other then 404 error', done => {
333+
const options = {ignoreNotFound: true};
334+
const error = new ApiError({code: 406, response: {} as r.Response});
335+
sandbox.stub(ServiceObject.prototype, 'request').callsArgWith(1, error);
336+
serviceObject.delete(options, (err, apiResponse_) => {
337+
assert.strictEqual(err, error);
338+
assert.strictEqual(apiResponse_, undefined);
339+
done();
340+
});
341+
});
342+
343+
it('should not pass ignoreNotFound to request', done => {
344+
const options = {ignoreNotFound: true};
345+
sandbox
346+
.stub(ServiceObject.prototype, 'request')
347+
.callsFake((reqOpts, callback) => {
348+
assert.strictEqual(reqOpts.qs.ignoreNotFound, undefined);
349+
done();
350+
callback(null, null, {} as r.Response);
351+
});
352+
serviceObject.delete(options, assert.ifError);
353+
});
354+
321355
it('should extend the defaults with request options', done => {
322356
const methodConfig = {
323357
reqOpts: {

0 commit comments

Comments
 (0)