Skip to content

Commit

Permalink
feat(api): api update (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Nov 3, 2024
1 parent a82334f commit 3e1cfa7
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 412 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ yarn-error.log
codegen.log
Brewfile.lock.json
dist
/deno
dist-deno
/*.tgz
.idea/

4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 9
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/nen-labs%2Fsteel-17f11d5403c9a4e9b70c9695f5289d851a195e9d33f182156dde77087c10d21f.yml
configured_endpoints: 6
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/nen-labs%2Fsteel-e2f35e13822cd958a10f588d91b72e19c31c4c6ad0f5c1fe7c8d13b8f03e6843.yml
49 changes: 18 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ const client = new Steel({
});

async function main() {
const params: Steel.ScrapeParams = {
url: 'https://www.eff.org/cyberspace-independence',
format: ['markdown'],
};
const response: Steel.ScrapeResponse = await client.scrape(params);
const session: Steel.Session = await client.sessions.create();
}

main();
Expand All @@ -69,17 +65,15 @@ a subclass of `APIError` will be thrown:
<!-- prettier-ignore -->
```ts
async function main() {
const response = await client
.scrape({ url: 'https://www.eff.org/cyberspace-independence', format: ['markdown'] })
.catch(async (err) => {
if (err instanceof Steel.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});
const session = await client.sessions.create().catch(async (err) => {
if (err instanceof Steel.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});
}

main();
Expand Down Expand Up @@ -114,7 +108,7 @@ const client = new Steel({
});

// Or, configure per-request:
await client.scrape({ url: 'https://www.eff.org/cyberspace-independence', format: ['markdown'] }, {
await client.sessions.create({
maxRetries: 5,
});
```
Expand All @@ -131,7 +125,7 @@ const client = new Steel({
});

// Override per-request:
await client.scrape({ url: 'https://www.eff.org/cyberspace-independence', format: ['markdown'] }, {
await client.sessions.create({
timeout: 5 * 1000,
});
```
Expand Down Expand Up @@ -183,17 +177,13 @@ You can also use the `.withResponse()` method to get the raw `Response` along wi
```ts
const client = new Steel();

const response = await client
.scrape({ url: 'https://www.eff.org/cyberspace-independence', format: ['markdown'] })
.asResponse();
const response = await client.sessions.create().asResponse();
console.log(response.headers.get('X-My-Header'));
console.log(response.statusText); // access the underlying Response object

const { data: response, response: raw } = await client
.scrape({ url: 'https://www.eff.org/cyberspace-independence', format: ['markdown'] })
.withResponse();
const { data: session, response: raw } = await client.sessions.create().withResponse();
console.log(raw.headers.get('X-My-Header'));
console.log(response.content);
console.log(session.id);
```

### Making custom/undocumented requests
Expand Down Expand Up @@ -297,12 +287,9 @@ const client = new Steel({
});

// Override per-request:
await client.scrape(
{ url: 'https://www.eff.org/cyberspace-independence', format: ['markdown'] },
{
httpAgent: new http.Agent({ keepAlive: false }),
},
);
await client.sessions.create({
httpAgent: new http.Agent({ keepAlive: false }),
});
```

## Semantic versioning
Expand Down
12 changes: 0 additions & 12 deletions api.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
# Steel

Types:

- <code><a href="./src/resources/top-level.ts">PdfResponse</a></code>
- <code><a href="./src/resources/top-level.ts">ScrapeResponse</a></code>
- <code><a href="./src/resources/top-level.ts">ScreenshotResponse</a></code>

Methods:

- <code title="post /v1/pdf">client.<a href="./src/index.ts">pdf</a>({ ...params }) -> PdfResponse</code>
- <code title="post /v1/scrape">client.<a href="./src/index.ts">scrape</a>({ ...params }) -> ScrapeResponse</code>
- <code title="post /v1/screenshot">client.<a href="./src/index.ts">screenshot</a>({ ...params }) -> ScreenshotResponse</code>

# Sessions

Types:
Expand Down
2 changes: 1 addition & 1 deletion scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ node scripts/utils/postprocess-files.cjs
(cd dist && node -e 'require("steel-sdk")')
(cd dist && node -e 'import("steel-sdk")' --input-type=module)

if command -v deno &> /dev/null && [ -e ./scripts/build-deno ]
if [ -e ./scripts/build-deno ]
then
./scripts/build-deno
fi
10 changes: 5 additions & 5 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export abstract class APIClient {
error: Object | undefined,
message: string | undefined,
headers: Headers | undefined,
) {
): APIError {
return APIError.generate(status, error, message, headers);
}

Expand Down Expand Up @@ -668,17 +668,17 @@ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
return await this.#client.requestAPIList(this.constructor as any, nextOptions);
}

async *iterPages() {
async *iterPages(): AsyncGenerator<this> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let page: AbstractPage<Item> = this;
let page: this = this;
yield page;
while (page.hasNextPage()) {
page = await page.getNextPage();
yield page;
}
}

async *[Symbol.asyncIterator]() {
async *[Symbol.asyncIterator](): AsyncGenerator<Item> {
for await (const page of this.iterPages()) {
for (const item of page.getPaginatedItems()) {
yield item;
Expand Down Expand Up @@ -721,7 +721,7 @@ export class PagePromise<
* console.log(item)
* }
*/
async *[Symbol.asyncIterator]() {
async *[Symbol.asyncIterator](): AsyncGenerator<Item> {
const page = await this;
for await (const item of page) {
yield item;
Expand Down
2 changes: 1 addition & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class APIError extends SteelError {
errorResponse: Object | undefined,
message: string | undefined,
headers: Headers | undefined,
) {
): APIError {
if (!status) {
return new APIConnectionError({ message, cause: castToError(errorResponse) });
}
Expand Down
73 changes: 15 additions & 58 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ import * as Pagination from './pagination';
import { type SessionsCursorParams, SessionsCursorResponse } from './pagination';
import * as Uploads from './uploads';
import * as API from './resources/index';
import * as TopLevelAPI from './resources/top-level';
import {
PdfParams,
PdfResponse,
ScrapeParams,
ScrapeResponse,
ScreenshotParams,
ScreenshotResponse,
} from './resources/top-level';
import {
Session,
SessionContext,
Expand Down Expand Up @@ -144,33 +135,6 @@ export class Steel extends Core.APIClient {

sessions: API.Sessions = new API.Sessions(this);

/**
* Generates a PDF from a specified webpage.
*/
pdf(body: TopLevelAPI.PdfParams, options?: Core.RequestOptions): Core.APIPromise<TopLevelAPI.PdfResponse> {
return this.post('/v1/pdf', { body, ...options });
}

/**
* Extracts content from a specified URL.
*/
scrape(
body: TopLevelAPI.ScrapeParams,
options?: Core.RequestOptions,
): Core.APIPromise<TopLevelAPI.ScrapeResponse> {
return this.post('/v1/scrape', { body, ...options });
}

/**
* Captures a screenshot of a specified webpage.
*/
screenshot(
body: TopLevelAPI.ScreenshotParams,
options?: Core.RequestOptions,
): Core.APIPromise<TopLevelAPI.ScreenshotResponse> {
return this.post('/v1/screenshot', { body, ...options });
}

protected override defaultQuery(): Core.DefaultQuery | undefined {
return this._options.defaultQuery;
}
Expand Down Expand Up @@ -207,19 +171,21 @@ export class Steel extends Core.APIClient {
static fileFromPath = Uploads.fileFromPath;
}

export const SteelError = Errors.SteelError;
export const APIError = Errors.APIError;
export const APIConnectionError = Errors.APIConnectionError;
export const APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;
export const APIUserAbortError = Errors.APIUserAbortError;
export const NotFoundError = Errors.NotFoundError;
export const ConflictError = Errors.ConflictError;
export const RateLimitError = Errors.RateLimitError;
export const BadRequestError = Errors.BadRequestError;
export const AuthenticationError = Errors.AuthenticationError;
export const InternalServerError = Errors.InternalServerError;
export const PermissionDeniedError = Errors.PermissionDeniedError;
export const UnprocessableEntityError = Errors.UnprocessableEntityError;
export {
SteelError,
APIError,
APIConnectionError,
APIConnectionTimeoutError,
APIUserAbortError,
NotFoundError,
ConflictError,
RateLimitError,
BadRequestError,
AuthenticationError,
InternalServerError,
PermissionDeniedError,
UnprocessableEntityError,
} from './error';

export import toFile = Uploads.toFile;
export import fileFromPath = Uploads.fileFromPath;
Expand All @@ -236,15 +202,6 @@ export declare namespace Steel {
type SessionsCursorResponse as SessionsCursorResponse,
};

export {
type PdfResponse as PdfResponse,
type ScrapeResponse as ScrapeResponse,
type ScreenshotResponse as ScreenshotResponse,
type PdfParams as PdfParams,
type ScrapeParams as ScrapeParams,
type ScreenshotParams as ScreenshotParams,
};

export {
Sessions as Sessions,
type Session as Session,
Expand Down
8 changes: 0 additions & 8 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,3 @@ export {
type SessionCreateParams,
type SessionListParams,
} from './sessions';
export {
type PdfResponse,
type ScrapeResponse,
type ScreenshotResponse,
type PdfParams,
type ScrapeParams,
type ScreenshotParams,
} from './top-level';
8 changes: 4 additions & 4 deletions src/resources/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,14 @@ export interface SessionCreateParams {
sessionId?: string;

/**
* Session timeout duration in milliseconds. Default is 900000 (15 minutes).
* Enable automatic captcha solving. Default is false.
*/
sessionTimeout?: number;
solveCaptcha?: boolean;

/**
* Enable automatic captcha solving. Default is false.
* Session timeout duration in milliseconds. Default is 900000 (15 minutes).
*/
solveCaptcha?: boolean;
timeout?: number;

/**
* Enable Steel-provided residential proxy usage for the browser session. Default
Expand Down
Loading

0 comments on commit 3e1cfa7

Please sign in to comment.