-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
137 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Build, Test and Publish | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build-and-publish: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 2 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20.x" | ||
registry-url: "https://registry.npmjs.org" | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
|
||
- name: Build | ||
run: npm run build | ||
|
||
- name: Run tests | ||
run: npm test | ||
|
||
- name: Check version change | ||
id: check | ||
run: | | ||
git diff HEAD^ HEAD -G"\"version\": \"[^\"]*\"" package.json | grep "\"version\":" || echo "No version change" | ||
if git diff HEAD^ HEAD -G"\"version\": \"[^\"]*\"" package.json | grep -q "\"version\":"; then | ||
echo "version_changed=true" >> $GITHUB_OUTPUT | ||
else | ||
echo "version_changed=false" >> $GITHUB_OUTPUT | ||
fi | ||
- name: Publish to npm | ||
if: steps.check.outputs.version_changed == 'true' | ||
run: npm publish --access public | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
testMatch: ['**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)'], | ||
testPathIgnorePatterns: ['/node_modules/', '/dist/'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Skrape, SkrapeError } from '../index'; | ||
import { z } from 'zod'; | ||
|
||
describe('Skrape', () => { | ||
let skrape: Skrape; | ||
|
||
beforeEach(() => { | ||
skrape = new Skrape({ | ||
apiKey: 'test-key', | ||
}); | ||
}); | ||
|
||
it('should create a Skrape instance', () => { | ||
expect(skrape).toBeInstanceOf(Skrape); | ||
}); | ||
|
||
it('should throw error if apiKey is missing', () => { | ||
expect(() => new Skrape({} as any)).toThrow(); | ||
}); | ||
|
||
it('should have correct default baseUrl', () => { | ||
expect((skrape as any).baseUrl).toBe('https://skrape.ai/api'); | ||
}); | ||
|
||
it('should store API key', () => { | ||
expect((skrape as any).apiKey).toBe('test-key'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Skrape, SkrapeError } from "./index"; | ||
import { z } from "zod"; | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
||
const apiKey = process.env.SKRAPE_API_KEY; | ||
if (!apiKey) { | ||
throw new Error("SKRAPE_API_KEY is required"); | ||
} | ||
|
||
const skrape = new Skrape({ | ||
apiKey, | ||
baseUrl: "http://localhost:3000/api", | ||
}); | ||
|
||
const newsSchema = z.object({ | ||
topStories: z | ||
.array( | ||
z.object({ | ||
title: z.string(), | ||
url: z.string(), | ||
score: z.number(), | ||
author: z.string(), | ||
commentCount: z.number(), | ||
}) | ||
) | ||
.max(3), | ||
}); | ||
|
||
async function test() { | ||
try { | ||
const result = await skrape.extract( | ||
"https://news.ycombinator.com", | ||
newsSchema | ||
); | ||
console.log(JSON.stringify(result, null, 2)); | ||
} catch (error) { | ||
if (error instanceof SkrapeError) { | ||
console.error("SkrapeError:", error.message); | ||
console.error("Status:", error.status); | ||
if (error.retryAfter) { | ||
console.error("Retry after:", error.retryAfter, "seconds"); | ||
} | ||
} else { | ||
console.error("Error:", error); | ||
} | ||
} | ||
} | ||
|
||
test(); |
This file was deleted.
Oops, something went wrong.