1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-14 07:30:05 +00:00

feat: added isUrl validation utility

This commit is contained in:
Yury Shkoda
2020-09-07 16:32:53 +03:00
parent 1f970e1102
commit 0eb9bc43ff
6 changed files with 33 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
import { isLogInRequired, needsRetry } from './utils' import { isLogInRequired, needsRetry, isUrl } from './utils'
import { CsrfToken } from './types/CsrfToken' import { CsrfToken } from './types/CsrfToken'
import { UploadFile } from './types/UploadFile' import { UploadFile } from './types/UploadFile'
@@ -11,7 +11,10 @@ export class FileUploader {
private jobsPath: string, private jobsPath: string,
private setCsrfTokenWeb: any, private setCsrfTokenWeb: any,
private csrfToken: CsrfToken | null = null private csrfToken: CsrfToken | null = null
) {} ) {
if (serverUrl) isUrl(serverUrl)
}
private retryCount = 0 private retryCount = 0
public uploadFile(sasJob: string, files: UploadFile[], params: any) { public uploadFile(sasJob: string, files: UploadFile[], params: any) {

View File

@@ -1,9 +1,13 @@
import { isUrl } from './utils'
/** /**
* A client for interfacing with the SAS9 REST API * A client for interfacing with the SAS9 REST API
* *
*/ */
export class SAS9ApiClient { export class SAS9ApiClient {
constructor(private serverUrl: string) {} constructor(private serverUrl: string) {
if (serverUrl) isUrl(serverUrl)
}
/** /**
* returns on object containing the server URL * returns on object containing the server URL

View File

@@ -2,7 +2,8 @@ import {
isAuthorizeFormRequired, isAuthorizeFormRequired,
parseAndSubmitAuthorizeForm, parseAndSubmitAuthorizeForm,
convertToCSV, convertToCSV,
makeRequest makeRequest,
isUrl
} from './utils' } from './utils'
import * as NodeFormData from 'form-data' import * as NodeFormData from 'form-data'
import * as path from 'path' import * as path from 'path'
@@ -26,7 +27,10 @@ export class SASViyaApiClient {
if (!rootFolderName) { if (!rootFolderName) {
throw new Error('Root folder must be provided.') throw new Error('Root folder must be provided.')
} }
if (serverUrl) isUrl(serverUrl)
} }
private csrfToken: CsrfToken | null = null private csrfToken: CsrfToken | null = null
private rootFolder: Folder | null = null private rootFolder: Folder | null = null
private sessionManager = new SessionManager( private sessionManager = new SessionManager(

View File

@@ -1,5 +1,5 @@
import { Session, Context, CsrfToken } from './types' import { Session, Context, CsrfToken } from './types'
import { asyncForEach, makeRequest } from './utils' import { asyncForEach, makeRequest, isUrl } from './utils'
const MAX_SESSION_COUNT = 1 const MAX_SESSION_COUNT = 1
@@ -8,7 +8,10 @@ export class SessionManager {
private serverUrl: string, private serverUrl: string,
private contextName: string, private contextName: string,
private setCsrfToken: (csrfToken: CsrfToken) => void private setCsrfToken: (csrfToken: CsrfToken) => void
) {} ) {
if (serverUrl) isUrl(serverUrl)
}
private sessions: Session[] = [] private sessions: Session[] = []
private currentContext: Context | null = null private currentContext: Context | null = null
private csrfToken: CsrfToken | null = null private csrfToken: CsrfToken | null = null

View File

@@ -13,3 +13,4 @@ export * from './parseSasViyaLog'
export * from './serialize' export * from './serialize'
export * from './splitChunks' export * from './splitChunks'
export * from './parseWeboutResponse' export * from './parseWeboutResponse'
export * from './isUrl'

12
src/utils/isUrl.ts Normal file
View File

@@ -0,0 +1,12 @@
export const isUrl = (url: string): boolean => {
const pattern = new RegExp(
'^(http://www.|https://www.|http://|https://)[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$',
'gi'
)
if (pattern.test(url)) return true
else
throw new Error(
`'${url}' is not a valid url. An example of a valid url is 'http://valid-url.com'.`
)
}