The TypeScript code sets up a role-based access control in a NestJS application using a custom guard. It defines a RolesGuard class that leverages metadata reflection to determine the necessary roles for accessing particular routes. It retrieves the user roles from the AWS API Gateway request context, specified in the headers, and checks if the user has at least one of the roles required to access a route, thus facilitating role-based authorization.
The TypeScript code uses AWS CDK to create an AWS API Gateway with a set of predefined REST API resources (like "signup", "login") each supporting specific HTTP methods. It sets up CORS, associates the API with a custom domain using a DNS certificate, and creates a DNS record in Route 53. The API integrates with a Lambda function handler and optionally protects resources with a Cognito User Pools authorizer. It configures a usage plan to throttle the API requests to a specified rate limit.
The code defines three classes: PineconeCrawler, DiskCrawler, and Neo4jCrawler, each utilizing the PuppeteerCrawler to scrape web pages from specified URLs. They process webpage content differently: storing embeddings in a Pinecone database, saving them locally with HNSWLib, and adding page metadata to a Neo4j graph database, respectively.
This TypeScript code defines a type alias ToTupleType that takes two type parameters: B, an object type, and F, a tuple of key strings representing keys of B. It maps over the tuple F, creating a new tuple type where each element is the type of the corresponding property in B. It essentially helps in transforming an object into a tuple type with a specific set of properties.
This TypeScript code defines an AnomalyDetector class that identifies anomalies in a dataset based on the specified sensitivity level. It uses a specified numeric key to extract values from dataset objects and compute the average and standard deviation of these values to identify anomalies, which are then filtered and can be retrieved using the getAnomalies method. It leverages an interface to type the options parameter used in the constructor to instantiate the class with necessary settings.
The TypeScript code defines a type alias OccurrenceMap and a function getDuplicates. The function takes a readonly array, calculates the occurrence of each element, and returns an object that maps each element (that appears more than once) to its frequency. It leverages TypeScript's utility types to work with both number and string arrays.
The TypeScript code defines two classes, Approach1 and Approach2, both implementing the IApproach interface, which takes IParams as input and returns IOutput. Another class, ServiceThatCombines, also implements IApproach, utilizing a map to dynamically choose between Approach1 and Approach2 based on the type parameter in IParams to execute the do method, facilitating a strategy pattern to determine the approach at runtime.
The TypeScript code defines an interface INiceAndLoose and a utility type Strict which transforms the properties of a type to be non-nullable and required. An instance of the Strict type, derived from INiceAndLoose, is created, showcasing that the properties must now be non-nullable and defined.
The TypeScript code defines a PropsProtector class to manage and validate property overrides through BaseProps and UserProps derived from an IProps interface. The AbstractClass sets immutable base properties, while ConcreteClass sets user properties. It then creates an instance of ConcreteClass, which logs a merged object of base and user properties, ensuring no base properties are overridden.
The TypeScript code defines a rate limiter using the leaky bucket algorithm. It has LeakyBucket and LeakyBucketTask classes. Tasks with a specified number of retries are added to the bucket and executed at intervals defined by rateMs. The bucket executes tasks one by one, retrying failed tasks until they succeed or exhaust their retries, and removes completed tasks. It allows for the addition of new tasks and stopping the task execution.