@socketsecurity/sdk
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -7,6 +7,14 @@ export type SocketSdkReturnType<T extends keyof import("./types/api").operations> = import('./types/api-helpers').OpReturnType<import('./types/api').operations[T]>; | ||
baseUrl?: string | undefined; | ||
userAgent?: string | undefined; | ||
}; | ||
export function createUserAgentFromPkgJson(pkgData: { | ||
name: string; | ||
version: string; | ||
homepage?: string; | ||
}): string; | ||
export class SocketSdk { | ||
constructor(apiKey: string, options?: SocketSdkOptions); | ||
createReportFromFilePaths(filePaths: string[], pathsRelativeTo?: string): Promise<SocketSdkResultType<'createReport'>>; | ||
createReportFromFilePaths(filePaths: string[], pathsRelativeTo?: string, issueRules?: { | ||
[key: string]: boolean; | ||
} | undefined): Promise<SocketSdkResultType<'createReport'>>; | ||
getScoreByNPMPackage(pkgName: string, version: string): Promise<SocketSdkResultType<'getScoreByNPMPackage'>>; | ||
@@ -13,0 +21,0 @@ getIssuesByNPMPackage(pkgName: string, version: string): Promise<SocketSdkResultType<'getIssuesByNPMPackage'>>; |
30
index.js
@@ -7,2 +7,4 @@ 'use strict' | ||
const pkg = require('./package.json') | ||
/** | ||
@@ -27,2 +29,3 @@ * @template {keyof import('./types/api').operations} T | ||
* @property {string} [baseUrl] | ||
* @property {string} [userAgent] | ||
*/ | ||
@@ -49,2 +52,3 @@ | ||
baseUrl = 'https://api.socket.dev/v0/', | ||
userAgent, | ||
} = options | ||
@@ -56,2 +60,5 @@ | ||
username: apiKey, | ||
headers: { | ||
'user-agent': (userAgent ? userAgent + ' ' : '') + createUserAgentFromPkgJson(pkg), | ||
}, | ||
...(agent ? { agent } : {}), | ||
@@ -81,5 +88,6 @@ } | ||
* @param {string} pathsRelativeTo | ||
* @param {{ [key: string]: boolean }} [issueRules] | ||
* @returns {Promise<SocketSdkResultType<'createReport'>>} | ||
*/ | ||
async createReportFromFilePaths (filePaths, pathsRelativeTo = '.') { | ||
async createReportFromFilePaths (filePaths, pathsRelativeTo = '.', issueRules) { | ||
const basePath = path.resolve(process.cwd(), pathsRelativeTo) | ||
@@ -89,3 +97,3 @@ const absoluteFilePaths = filePaths.map(filePath => path.resolve(basePath, filePath)) | ||
const [ | ||
{ FormData }, | ||
{ FormData, Blob }, | ||
{ fileFromPath }, | ||
@@ -101,2 +109,7 @@ client | ||
if (issueRules) { | ||
const issueRulesBlob = new Blob([JSON.stringify(issueRules)], { type: 'application/json' }) | ||
body.set('issueRules', issueRulesBlob, 'issueRules') | ||
} | ||
const files = await Promise.all(absoluteFilePaths.map(absoluteFilePath => fileFromPath(absoluteFilePath))) | ||
@@ -247,2 +260,13 @@ | ||
module.exports = { SocketSdk } | ||
/** | ||
* @param {{ name: string, version: string, homepage?: string }} pkgData Package.json data to base the User-Agent on | ||
* @returns {string} | ||
*/ | ||
function createUserAgentFromPkgJson (pkgData) { | ||
return `${pkgData.name.replace('@', '').replace('/', '-')}/${pkgData.version}` + (pkgData.homepage ? ` (${pkgData.homepage})` : '') | ||
} | ||
module.exports = { | ||
createUserAgentFromPkgJson, | ||
SocketSdk, | ||
} |
{ | ||
"name": "@socketsecurity/sdk", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "SDK for the Socket API client", |
# @socketsecurity/sdk | ||
[![Socket Badge](https://socket.dev/api/badge/npm/pkg/@socketsecurity/sdk)](https://socket.dev/npm/package/@socketsecurity/sdk) | ||
[![npm version](https://img.shields.io/npm/v/@socketsecurity/sdk.svg?style=flat)](https://www.npmjs.com/package/@socketsecurity/sdk) | ||
@@ -50,5 +51,6 @@ [![TypeScript types](https://img.shields.io/npm/types/@socketsecurity/sdk.svg?style=flat)](https://www.npmjs.com/package/@socketsecurity/sdk) | ||
* `createReportFromFilePaths(filePaths, pathsRelativeTo=.)` | ||
* `createReportFromFilePaths(filePaths, pathsRelativeTo=., [issueRules])` | ||
* `filePaths`: An `array` of absolute or relative `string` paths to `package.json` and any corresponding `package-lock.json` files | ||
* `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 | ||
* `issueRules`: An object that follows the format of the [`socket.yml`](https://docs.socket.dev/docs/socket-yml) issue rules. Keys being issue names, values being a boolean that activates or deactivates it. Is applied on top of default config and organization config. | ||
* `getReportList()` | ||
@@ -62,2 +64,35 @@ * `getReport(id)` | ||
## Additional exports | ||
* `createUserAgentFromPkgJson(pkgJson)` | ||
* `pkgJson`: The content of the `package.json` you want to create a `User-Agent` string for | ||
## Advanced | ||
### Specifying custom user agent | ||
The `SocketSdk` constructor accepts an `options` object as its second argument and there a `userAgent` key with a string value can be specified. If specified then that user agent will be prepended to the SDK user agent. See this example: | ||
```js | ||
const client = new SocketSdk('yourApiKeyHere', { | ||
userAgent: 'example/1.2.3 (http://example.com/)' | ||
}) | ||
``` | ||
Which results in the [HTTP `User-Agent` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent): | ||
``` | ||
User-Agent: example/1.2.3 (http://example.com/) socketsecurity-sdk/0.5.2 (https://github.com/SocketDev/socket-sdk-js) | ||
``` | ||
To easily create a user agent for your code you can use the additional export `createUserAgentFromPkgJson()` like this, assuming `pkgJson` contains your parsed `package.json`: | ||
```js | ||
const client = new SocketSdk('yourApiKeyHere', { | ||
userAgent: createUserAgentFromPkgJson(pkgJson) | ||
}) | ||
``` | ||
Specifying a custom user agent is good practice when shipping a piece of code that others can use to make requests. Eg. [our CLI](https://github.com/SocketDev/socket-cli-js) uses this option to identify requests coming from it + mentioning which version of it that is used. | ||
## See also | ||
@@ -68,1 +103,2 @@ | ||
* [Socket GitHub App](https://github.com/apps/socket-security) | ||
* [Socket CLI](https://github.com/SocketDev/socket-cli-js) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75484
2314
102