Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Actor.getInputOrThrow() method #198

Merged
merged 1 commit into from
May 31, 2023
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
9 changes: 3 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module.exports = {
collectCoverage: false,
testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'],
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.ts$': ['ts-jest', {
tsconfig: 'test/tsconfig.json',
}],
},
collectCoverageFrom: [
'<rootDir>/packages/*/src/**/*.[jt]s',
Expand All @@ -19,9 +21,4 @@ module.exports = {
'dist/package.json',
'<rootDir>/package.json',
],
globals: {
'ts-jest': {
tsconfig: 'test/tsconfig.json',
},
},
};
22 changes: 22 additions & 0 deletions packages/apify/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,20 @@ export class Actor<Data extends Dictionary = Dictionary> {
return input;
}

/**
* Gets the actor input value just like the {@apilink Actor.getInput} method,
* but throws if it is not found.
*/
async getInputOrThrow<T = Dictionary | string | Buffer>(): Promise<T> {
const input = await this.getInput<T>();

if (input == null) {
throw new Error('Input does not exist');
}

return input;
}

/**
* Opens a key-value store and returns a promise resolving to an instance of the {@apilink KeyValueStore} class.
*
Expand Down Expand Up @@ -1323,6 +1337,14 @@ export class Actor<Data extends Dictionary = Dictionary> {
return Actor.getDefaultInstance().getInput();
}

/**
* Gets the actor input value just like the {@apilink Actor.getInput} method,
* but throws if it is not found.
*/
static async getInputOrThrow<T = Dictionary | string | Buffer>(): Promise<T> {
return Actor.getDefaultInstance().getInputOrThrow<T>();
}

/**
* Opens a key-value store and returns a promise resolving to an instance of the {@apilink KeyValueStore} class.
*
Expand Down
3 changes: 3 additions & 0 deletions test/apify/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,9 @@ describe('Actor', () => {
const TestingActor = new Actor();

test('should work', async () => {
await expect(TestingActor.getInput()).resolves.toBeNull();
await expect(TestingActor.getInputOrThrow()).rejects.toThrowError('Input does not exist');

const mockGetValue = jest.spyOn(TestingActor, 'getValue');
mockGetValue.mockImplementation(async (key) => expect(key).toEqual(KEY_VALUE_STORE_KEYS.INPUT));

Expand Down