Skip to content

Commit f1a9014

Browse files
author
Pelle Wessman
authored
Rewrite with handcrafted class (#13)
* Rewrite with handcrafted class Replaces `api` module with a simple handcrafted API class that wraps `got` and which simply auto-generates the types * Add husky + readd lockfile * Extend static code analysis + tests * Fix npm lint workflow test script name * Fix path to openapi.json * Update README * Restore prettification of base JSON
1 parent 08d91df commit f1a9014

27 files changed

+7031
-344923
lines changed

.eslintignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1+
/coverage/**/*
12
/dist
2-
/index.d.ts
3-
/index.js
4-
/src
3+
/types/api.d.ts

.eslintrc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
"extends": "@socketsecurity",
33
"root": true,
44
"parserOptions": {
5-
"project": [
6-
"./build/tsconfig.json",
7-
"./tests/tsconfig.json"
8-
]
5+
"project": "./tsconfig.json"
96
},
107
"rules": {
118
"no-console": "warn"

.github/workflows/ci.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,10 @@ jobs:
2020
name: "Linting"
2121
uses: SocketDev/workflows/.github/workflows/reusable-base.yml@master
2222
with:
23-
npm-test-script: 'lint'
24-
test-old:
25-
name: "Tests"
26-
uses: SocketDev/workflows/.github/workflows/reusable-base.yml@master
27-
with:
28-
npm-test-script: 'ci-test-old'
29-
node-versions: '14,16'
23+
npm-test-script: 'check'
3024
test:
3125
name: "Tests"
3226
uses: SocketDev/workflows/.github/workflows/reusable-base.yml@master
3327
with:
3428
npm-test-script: 'ci-test'
35-
node-versions: '18,19'
29+
node-versions: '14,16,18,19'

.gitignore

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
# Basic ones
2+
/coverage
3+
/coverage-ts
4+
/node_modules
5+
/.env
6+
/.nyc_output
7+
/yarn.lock
8+
9+
# Generated types
10+
*.d.ts
11+
*.d.ts.map
12+
!api*.d.ts
13+
14+
# Library specific ones
115
/.api
2-
dist
3-
node_modules
4-
.nyc_output
16+
/dist

.husky/pre-push

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npm test

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,48 @@ npm install @socketsecurity/sdk
1616
### ESM / TypeScript
1717

1818
```javascript
19-
import { socketSdk } from '@socketsecurity/sdk'
19+
import { SocketSdk } from '@socketsecurity/sdk'
2020

21-
const client = socketSdk.auth('yourApiKeyHere')
21+
const client = new SocketSdk('yourApiKeyHere')
2222

23-
const res = await client.getIssuesByNPMPackage({
24-
package: '@socketsecurity/eslint-config',
25-
version: '1.0.0'
26-
})
23+
const res = await client.getQuota()
24+
25+
if (res.success) {
26+
// Will output { quota: 123 } if the quota you have left is 123
27+
console.log(res.data)
28+
}
2729
```
2830

2931
### CommonJS
3032

3133
```javascript
32-
const { socketSdk } = require('@socketsecurity/sdk')
34+
const { SocketSdk } = require('@socketsecurity/sdk')
3335
```
3436

37+
## SocketSdk Methods
38+
39+
### Package methods
40+
41+
* `getIssuesByNPMPackage(packageName, version)`
42+
* `packageName`: A `string` representing the name of the npm package you want the issues for
43+
* `version`: A `string` representing the version of the npm package to return the issues for
44+
* `getScoreByNPMPackage(packageName, version)`
45+
* `packageName`: A `string` representing the name of the npm package you want the score for
46+
* `version`: A `string` representing the version of the npm package to return the score for
47+
48+
### Report methods
49+
50+
* `createReportFromFilePaths(filePaths, pathsRelativeTo=.)`
51+
* `filePaths`: An `array` of absolute or relative `string` paths to `package.json` and any corresponding `package-lock.json` files
52+
* `pathsRelativeTo`: A `string` path that the absolute paths `filePaths` are relative to. This to calculate where in your project the `package.json`/`package-lock.json` files lives
53+
* `getReportList()`
54+
* `getReport(id)`
55+
* `id`: A `string` representing the id of a created report
56+
57+
### Utility methods
58+
59+
* `getQuota()`
60+
3561
## See also
3662

3763
* [Socket API Reference](https://docs.socket.dev/reference)

build/generate-sdk.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

build/generate-types.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import openapiTS from 'openapi-typescript'
2+
3+
const localPath = new URL('../openapi.json', import.meta.url)
4+
5+
const output = await openapiTS(localPath, {
6+
formatter (node) {
7+
if (node.format === 'binary') {
8+
return 'never'
9+
}
10+
}
11+
})
12+
13+
// eslint-disable-next-line no-console
14+
console.log(output)

build/minify-dist-json.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

build/prettify-base-json.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
/* eslint-disable no-console */
2-
// @ts-check
1+
import { readFile, writeFile } from 'node:fs/promises'
2+
import path from 'node:path'
33

4-
const { readFile, writeFile } = require('node:fs/promises')
5-
const path = require('node:path')
4+
const openApiData = await readFile(path.join(__dirname, '../openapi.json'), 'utf8')
65

7-
Promise.resolve().then(async () => {
8-
const openApiData = await readFile(path.join(__dirname, '../openapi.json'), 'utf8')
9-
10-
await writeFile(path.join(__dirname, '../openapi.json'), JSON.stringify(JSON.parse(openApiData), undefined, 2))
11-
}).catch(err => {
12-
console.error('Failed with error:', err.stack)
13-
process.exit(1)
14-
})
6+
await writeFile(path.join(__dirname, '../openapi.json'), JSON.stringify(JSON.parse(openApiData), undefined, 2))

0 commit comments

Comments
 (0)