Skip to content

Commit 5fbbfe4

Browse files
authored
feat: add Actor.getInputOrThrow() method (#198)
1 parent 7d2fc8e commit 5fbbfe4

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

jest.config.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ module.exports = {
66
collectCoverage: false,
77
testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'],
88
transform: {
9-
'^.+\\.ts$': 'ts-jest',
9+
'^.+\\.ts$': ['ts-jest', {
10+
tsconfig: 'test/tsconfig.json',
11+
}],
1012
},
1113
collectCoverageFrom: [
1214
'<rootDir>/packages/*/src/**/*.[jt]s',
@@ -19,9 +21,4 @@ module.exports = {
1921
'dist/package.json',
2022
'<rootDir>/package.json',
2123
],
22-
globals: {
23-
'ts-jest': {
24-
tsconfig: 'test/tsconfig.json',
25-
},
26-
},
2724
};

packages/apify/src/actor.ts

+22
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,20 @@ export class Actor<Data extends Dictionary = Dictionary> {
726726
return input;
727727
}
728728

729+
/**
730+
* Gets the actor input value just like the {@apilink Actor.getInput} method,
731+
* but throws if it is not found.
732+
*/
733+
async getInputOrThrow<T = Dictionary | string | Buffer>(): Promise<T> {
734+
const input = await this.getInput<T>();
735+
736+
if (input == null) {
737+
throw new Error('Input does not exist');
738+
}
739+
740+
return input;
741+
}
742+
729743
/**
730744
* Opens a key-value store and returns a promise resolving to an instance of the {@apilink KeyValueStore} class.
731745
*
@@ -1323,6 +1337,14 @@ export class Actor<Data extends Dictionary = Dictionary> {
13231337
return Actor.getDefaultInstance().getInput();
13241338
}
13251339

1340+
/**
1341+
* Gets the actor input value just like the {@apilink Actor.getInput} method,
1342+
* but throws if it is not found.
1343+
*/
1344+
static async getInputOrThrow<T = Dictionary | string | Buffer>(): Promise<T> {
1345+
return Actor.getDefaultInstance().getInputOrThrow<T>();
1346+
}
1347+
13261348
/**
13271349
* Opens a key-value store and returns a promise resolving to an instance of the {@apilink KeyValueStore} class.
13281350
*

test/apify/actor.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,9 @@ describe('Actor', () => {
950950
const TestingActor = new Actor();
951951

952952
test('should work', async () => {
953+
await expect(TestingActor.getInput()).resolves.toBeNull();
954+
await expect(TestingActor.getInputOrThrow()).rejects.toThrowError('Input does not exist');
955+
953956
const mockGetValue = jest.spyOn(TestingActor, 'getValue');
954957
mockGetValue.mockImplementation(async (key) => expect(key).toEqual(KEY_VALUE_STORE_KEYS.INPUT));
955958

0 commit comments

Comments
 (0)